如何用python生成数据帧子集函数

2021-11-29 22:37:01 发布

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

我是熊猫的新手。我有一个包含每个州人口统计数据的数据框。我试图创建一个函数,它接受df和行列表,在本例中是状态列表,并为每个状态返回df。我想我的逻辑错了,因为我犯了个错误。提前谢谢。你知道吗

这是我的df的一个例子:

     State   Year   Deaths          
0   Alabama 1999    39  
1   Alabama 2000    46  
2   Alabama 2001    67  
3   Alabama 2002    75  

分享我在下面尝试的功能:

def subseting(df ,list_of_states):
    df_copy = df.copy()
    for i in list_of_states:
         if i == df_copy.State :
                df_copy = df[df.State == i]
                df_copy = df_copy[[ 'Year' , 'Deaths']]
    return df_copy

a = ['Alabama' , 'Alaska' , 'Arizona ']

print(subseting(df, a))

在下面分享我的错误:

ValueError  Traceback (most recent call last)
<ipython-input-304-3528e6a59ccf> in <module>
      1 a = ['Alabama' , 'Alaska' , 'Arizona ']
      2 
----> 3 print(subseting_44(df, a))

<ipython-input-303-faa8c8e91e86> in subseting_44(df, list_of)
      2     df_copy = df.copy()
      3     for i in list_of:
----> 4         if i == df.State:
      5 #     df_copy= df[df.State == list_of]
      6 

/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
   1476         raise ValueError("The truth value of a {0} is ambiguous. "
   1477                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478                          .format(self.__class__.__name__))
   1479 
   1480     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().