繁体   English   中英

Python如何将分组分组为合并的数据帧?

[英]Python how to groupby into colsolidated dataframe?

这是我现在的代码:

d = {}
for stage in ['doggo', 'floofer', 'puppo', 'pupper']:
    #d[stage] =df.groupby([stage]).agg({'retweet_count': 'sum'})
    d[stage] = df.groupby(stage)['retweet_count'].sum()
stage_retweets = pd.DataFrame.from_dict(d)

它产生这个:

         doggo      floofer     puppo       pupper
None    1387471.0   1517639.0   1472697.0   1444766.0
doggo   159188.0    NaN         NaN         NaN
floofer NaN         29020.0     NaN         NaN
puppo   NaN         NaN         73962.0     NaN
pupper  NaN         NaN         NaN         101893.0

我真正想制作的是:

         doggo      floofer     puppo       pupper
None    1387471.0   1517639.0   1472697.0   1444766.0
stage   159188.0    29020.0     73962.0     101893.0     

有谁知道如何做到这一点?

d = {}
# 1 - Put your stages in a list variable
stages = ['doggo', 'floofer', 'puppo', 'pupper']

for stage in stages:
    d[stage] = df.groupby(stage)['retweet_count'].sum()
stage_retweets = pd.DataFrame.from_dict(d)
print(stage_retweets)

# 2 - Create a column conditionally to detect if the index in stages list or not
# !! important !! make shure you have only one index level otherwise stage_retweets.index.isin(stages) won't work
stage_retweets['is_stage'] = np.where(stage_retweets.index.isin(stages), 'Stage', 'None')
print(stage_retweets)

# 3 - Groupby this new column
stage_retweets = stage_retweets.groupby('is_stage').sum().reset_index()
print(stage_retweets)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM