Selecting multiple columns in a pandas dataframe

后端 未结 19 1793
醉话见心
醉话见心 2020-11-22 00:08

I have data in different columns but I don\'t know how to extract it to save it in another variable.

index  a   b   c
1      2   3   4
2      3   4   5
         


        
19条回答
  •  醉话见心
    2020-11-22 00:35

    df[['a', 'b']]  # select all rows of 'a' and 'b'column 
    df.loc[0:10, ['a', 'b']]  # index 0 to 10 select column 'a' and 'b'
    df.loc[0:10, 'a':'b']  # index 0 to 10 select column 'a' to 'b'
    df.iloc[0:10, 3:5]  # index 0 to 10 and column 3 to 5
    df.iloc[3, 3:5]  # index 3 of column 3 to 5
    

提交回复
热议问题