我该如何从一个从Excel文件创建的Dataframe中提取列匹配特定值的行?
以下是Dataframe中的几行:
Food Men Women
0 Total fruit 86.20 88.26
1 Apples, Total 89.01 89.66
2 Apples as fruit 89.18 90.42
3 Apple juice 88.78 88.42
4 Bananas 95.42 94.18
5 Berries 84.21 81.73
6 Grapes 88.79 88.13
以下是我用来读取Excel文件,并选择所需列和适当重命名的代码:
data1= pd.read_excel('USFoodCommodity.xls', sheetname='94-98 FAH', skiprows=76,skip_footer=142, parse_cols='A, H, K')
data1.columns = ['Food', 'Men', 'Women']
# Try 1: data1 = data1[data1['Food'].isin(['Total fruit']) == True] works
# Try 2: data1 = data1[data1['Food'].isin(['Apple, Total']) == True] doesn't work
# Try 3: data1 = data1.iloc[[1]] returns Apples, Total but not appropriate to use integer index
# Try 4: data1[data1['Food'] == 'Berries'] doesn't work
到目前为止,根据像这里、这里或者这里的回答,我只能够返回第一个"Total fruit"的索引。当我尝试使用以上的其他方法时,我只会得到例如列名之类的结果:
Food Men Women
我刚接触pandas,但是不知道哪里出了问题。为什么我可以提取Food == Total fruit的第一行,但是其他的行就不行呢?