【问题标题】:How to Group by from the list of data in the column and do the operation如何从列中的数据列表中分组并进行操作
【发布时间】:2020-01-01 18:30:13
【问题描述】:

下面是df,我需要分析数据。

   gender   dob          list
0   M   01/01/87    [['Office/Work'],['31-35'], ['Salaried']]
1   M   01/01/94    [[Movies,Restaurants'],['21-25'], ['Salaried']]
2   M   01/01/95    [['College/Park'],['21-25'],['Student']] 
3   F   01/01/97    [['College'], ['21-25'], ['Student']]

预期结果 1. 我需要分析数据集中有多少薪水

df['受薪']

Total = 2, Male = 2, Female = 0

  1. 列表中有多少学生 df['学生']

Total = 2, Male = 1, Female = 1

  1. 有多少人去看电影 df['电影']

Total = 1, Male = 1, Female=0

  1. 按不同年龄段分组 df['age_group']

    Age_Group Total Male Female ['21-25'] 3 2 1 ['31-35'] 1 1 0

  2. 男女比例是多少

round(len(df.loc[df['gender'] == 'M']) / (len(df.loc[df['gender'] == 'M']) + len(df.loc[df['gender'] == 'F'])),2)*100

【问题讨论】:

  • 这是家庭作业吗?
  • @ignoring_gravity,这实际上是一个问题。我已经完成了很长的解决方案,但没有达到标准。卡住了,所以提出一个问题。你可以处理这个问题
  • 您希望最终的 df 看起来如何。我知道你已经发布了预期的结果,但是如果你可以发布预期的数据框,那会有所帮助

标签: pandas


【解决方案1】:

您可以使用explode 将列的元素列表拆分为行。

df=pd.DataFrame({'gender':['M','M','M','F'],'B':[[['Office/Work'],['31-35'], ['Salaried']],[['Movies,Restaurants'],['21-25'], ['Salaried']],[[
'College/Park'],['21-25'],['Student']],[['College'], ['21-25'], ['Student']]]}) 

df:

  gender                                            B
0      M         [[Office/Work], [31-35], [Salaried]]
1      M  [[Movies,Restaurants], [21-25], [Salaried]]
2      M         [[College/Park], [21-25], [Student]]
3      F              [[College], [21-25], [Student]]

x=df.explode('B')

x:

  gender                     B
0      M         [Office/Work]
0      M               [31-35]
0      M            [Salaried]
1      M  [Movies,Restaurants]
1      M               [21-25]
1      M            [Salaried]
2      M        [College/Park]
2      M               [21-25]
2      M             [Student]
3      F             [College]
3      F               [21-25]
3      F             [Student]

x['B']=x.B.astype(str) 
final_df=x.groupby(['B','gender']).size().unstack(fill_value=0)  

final_df:

gender                  F  M
B                           
['21-25']               1  2
['31-35']               0  1
['College']             1  0
['College/Park']        0  1
['Movies,Restaurants']  0  1
['Office/Work']         0  1
['Salaried']            0  2
['Student']             1  1

您可以使用 F、M 列计算总计。

【讨论】:

  • df_final_resp.assign(final_resp=df_final_resp.final_resp.str.split('],').replace('[','')).explode('final_resp').head() 你的代码对我不起作用,我已经完成了上面的一个,你能告诉我可能是什么原因
  • 您可以使用df['col'].apply(', '.join) 代替df_final_resp.final_resp.str.split('],').replace('[','')) source:stackoverflow.com/questions/37347725/…
  • 它不起作用的原因是因为您将列表作为字符串。爆炸适用于列表。所以你应该像我一样先爆炸。如果您不想要括号,请执行以下操作:x['B'] =x['B'].apply(''.join)
  • 只需将x['B']=x.B.astype(str) 替换为x['B'] =x['B'].apply(''.join) 即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2021-05-23
  • 1970-01-01
  • 2021-02-11
  • 2021-10-05
  • 2017-08-26
  • 2014-11-04
相关资源
最近更新 更多