因此,我试图根据多种条件从DF中过滤数据。我在网上进行了研究,但没有找到可行的解决方案。
我发现了2种我理解的方法,但它们也不起作用。 而且,当我使用Excel过滤数据时,我可以看到有一些基于我的条件的数据,但是在Python中,我不确定为什么我的代码无法正常工作。
任何帮助将不胜感激!
# need:
# Value = everything
# Ethnicity = everything
# based on:
# Time = 2004
# Region = All
# Age = All
# Sex = All
# method 1
a = df.loc[(df.Time == "2004") & (df.Region == "All") & (df.Age == "All") & (df.Sex == "All"), ["Ethnicity","Value"]]
print(a)
# method 2
b = np.where((df.Time == "2004") & (df.Region == "All") & (df.Age == "All") & (df.Sex == "All"))
print(df.loc[b])
Output:
Empty DataFrame
Columns: [Ethnicity, Value]
Index: []
Empty DataFrame
Columns: [Measure, Measure_type, Ethnicity, Ethnicity_type, Time, Time_Type, Region, Age, Age_Type, Sex, Value, confidence_interval, Numerator, denominator, samp_size]
Index: []
Excel数据过滤显示根据我的条件存在数据,但是如何在Python中做到这一点? See the screenshot from Excel
答案 0 :(得分:0)
您只能使用像这样的Dataframe基本过滤语法(您有一些错误):
df[(df['Time'] == 2004) & (df['Region'] == "All") & (df['Age'] == "All") & (df['Sex'] == "All")]
(df.Time == "2004")
,在将excel工作表导入为数据框时,这些数据会在int
处自动转换为正确的类型,因此您需要(df.Time == 2004)
["Ethnicity","Value"]
答案 1 :(得分:0)
a = df [(df.Time ==“ 2004”)&(df.Region ==“ All”)&(df.Age ==“ All”)&(df.Sex ==“ All”) ] [“种族”,“价值”]