如何仅在一个级别中在 Pandas 中创建组 [英] how to crate the group by in pandas only in one level
本文介绍了如何仅在一个级别中在 Pandas 中创建组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的 excel 文件中导入以下 df3 数据框,并且只想对名称进行分组,其余重复数据应反映如下.
I am importing below df3 dataframe in my excel file and want to grouby only Name and rest dublicate data should reflect as below .
注意(每个月的数据将按月添加.)
Note (Each Month data will be added as per month wise. )
Df3 =pd.read_Excel('Data')
print (df3)
Name ID Month Shift
Jon 1 Feb A
Jon 1 Jan B
Jon 1 Mar C
Mike 1 Jan A
Mike 1 Jan B
Jon 1 Feb C
Jon 1 Jan A
并且我希望在相同的格式中具有如下输出.请帮助我,因为我卡在这里.
and i want to have output like as below in the same formate . Please help me on same as im stuck here .
将非常感谢您的帮助和支持.
Will be greatfull for help and support .
推荐答案
你可以通过
df=df.iloc[pd.to_datetime(df.Month,format='%b').argsort()]
df=pd.concat([pd.DataFrame({'Month':[x] }).append(y).fillna('').append(pd.DataFrame(dict.fromkeys(y.columns,['']))) for x , y in df.groupby('Name')]).drop('Name',1).iloc[:-1]
print(df)
Month ID Shift
0 Jon
1 Jan 1 B
6 Jan 1 A
0 Feb 1 A
5 Feb 1 C
2 Mar 1 C
0
0 Mike
3 Jan 1 A
4 Jan 1 B
这篇关于如何仅在一个级别中在 Pandas 中创建组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文