如何在pandas中通过索引过滤和打印整行

2024-10-03 00:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试图打印基于index values的行时遇到了一些问题

>>>in: print(df)  # My table when I print df

>>>out:
Item          Column1       Column2  Column3   Column4     Column5
Fooooo        64.318        7.574      0.471    0.364      -   
Yooooo        56.839        7.252    (1.914)  (1.945)      (0.015)   
Zaaaaa        45.275        7.186    (7.109)  (1.611)      (0.016)   
Zorrrr         0.381        0.063    (5.305)    (1.6)      (0.017)   
Kaarrr         1.325         0.26    (5.514)  (2.563)      (0.019) 

预期结果

如何通过index分离df,从而产生下面的结果?你知道吗

>>>in: print(df index['Fooooo'])  # For example
>>>out:
Item          Column1       Column2  Column3   Column4     Column5
Fooooo        64.318        7.574      0.471    0.364      -   

>>>in: print(df index['Yooooo'])  # For example
>>>out:
Item          Column1       Column2  Column3   Column4     Column5
Yooooo        56.839        7.252    (1.914)  (1.945)      (0.015)

>>>in: print(df index['Zaaaaa'])  # For example
>>>out:
Item          Column1       Column2  Column3   Column4     Column5  
Zaaaaa        45.275        7.186    (7.109)  (1.611)      (0.016)  

我试过了

print(df.index[0])-这只是给了我Item。不会与Columns的其余部分一起出现


Tags: indfforindexoutitemprintcolumn1
2条回答

您可以使用ix

In[31]:df.ix['Fooooo']
Out[31]: 
Column1    64.318
Column2     7.574
Column3     0.471
Column4     0.364
Column5         -

尝试筛选方法:

df[df.index == 'Fooooo']

          Column1  Column2 Column3 Column4 Column5
Item                                            
Fooooo   64.318    7.574   0.471   0.364       -

相关问题 更多 >