简体   繁体   English

循环 dataframe 并在每次迭代中提取不同的列

[英]Looping over dataframe with extracting different columns at each iteration

I am trying to loop over a dataframe df and I would like to extract different columns at each iteration.我正在尝试遍历 dataframe df,我想在每次迭代中提取不同的列。

say I have Columns: ['A', 'B', 'C', 'D', 'E', 'F'] in my df说我有 Columns: ['A', 'B', 'C', 'D', 'E', 'F']在我的 df

column_names=[['A','B'],['A','C','D']]

    for index,row in df:  //lets assume index starts with 0
       row[column_names[index]] // However you can not apply this syntax for rows like you could for a df to get a sub dataframe. 

What are my options?我有哪些选择? I have tried itertuples and iterrows but you can not select different columns by passing a list of column names我已经尝试过 itertuples 和 iterrows 但你不能通过传递列名列表来 select 不同的列

Thanks谢谢

The easiest way to loop over columns and retrieving a dataframe would be to invert your loops:循环遍历列并检索 dataframe 的最简单方法是反转循环:

for col in column_names:
    for ix in df.index:
        print(df.loc[ix, col])

With iterrows() you get a tuple with index at 0th position and row at the 1st.使用iterrows()你会得到一个索引在第 0 个 position 和第 1 行的元组。 You might want to use iterrows() as:您可能希望将iterrows()用作:

column_names=[['A',"B"],['A','C','D']]
for row in df.iterrows():
    print(row[1][column_names[row[0]]].to_frame())

For a df of ones ie:对于一个df,即:

    A   B   C   D   E   F
0   1.0 1.0 1.0 1.0 1.0 1.0
1   1.0 1.0 1.0 1.0 1.0 1.0

You get:你得到:

A    1.0
B    1.0
Name: 0, dtype: float64
A    1.0
C    1.0
D    1.0
Name: 1, dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM