Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

python - How to iterate over rows of a Frame in DataTable

With pandas I usually iterate the rows of a DataFrame with itertuples or iterrows. How can I do this kind of iteration on a Frame from Python DataTable?

Exemple of Pandas iteration that I need:

for row in df_.itertuples():
    print(row)
question from:https://stackoverflow.com/questions/65849569/how-to-iterate-over-rows-of-a-frame-in-datatable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the documentation for DataTables:

A Frame is column-oriented in the sense that internally the data is stored separately for each column. Each column has its own name and type. Types may be different for different columns but cannot vary within each column.

That being said, you can iterate over rows as:

from datatable import dt, f, by, g, join, sort, update, ifelse

data = {"A": [1, 2, 3, 4, 5],
        "B": [4, 5, 6, 7, 8],
        "C": [7, 8, 9, 10, 11],
        "D": [5, 7, 2, 9, -1]}

# datatable
DT = dt.Frame(data)

# select single row
print(DT[2, :])

# Select several rows by their indices
print(DT[[2,3,4], :])

# Select a slice of rows by position
print(DT[2:5, :])

# Select rows on multiple conditions, using OR
print(DT[(f.A>3) | (f.B<5), :])

For more row iteration examples and comparison with Pandas DataFrame, have a look at this official page. In case you have some issues, you may raise the issue over at DataTable official github repo.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...