gpt4 book ai didi

python - 在 Python 中格式化聚合数据帧的 header

转载 作者:行者123 更新时间:2023-12-01 09:00:25 25 4
gpt4 key购买 nike

我有一个数据框如下:-

,issue_name,doc_id,doc_type,doc_title
0,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device
1,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device
2,The App keeps crashing / restarting / hanging,5b51a24d9b989aaf3db0691a,GUIDE,Fix the App
3,The App keeps crashing / restarting / hanging,5b51a24d9b989aaf3db0691a,GUIDE,Fix the App
4,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device
5,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device

当我使用以下代码聚合相同的计数时:-

dfreturns = pd.DataFrame(Guidedocdetails, columns=['issue_name','doc_id','doc_type','doc_title'])
dfreturns.to_csv('ReturnGuideDocDetails.csv')
dfreturnguidecount = dfreturns.groupby(['issue_name','doc_type','doc_title']).agg(['count'])
dfreturnguidecount.to_csv('Return_guideid_counts.csv')

我得到的输出如下:enter image description here

如何删除顶部的 doc_id 和多余行。我想要的输出如下: enter image description here

请帮助我了解如何实现同样的目标。

应用以下代码后:-

dfnonreturnguidecount = (dfnonreturns.groupby(['issue_name','doc_type','doc_title'])['issue_name'].count().reset_index(name='count'))
dfnonreturnguidecount.to_csv('NonReturn_guideid_counts.csv')

输出:- enter image description here

最佳答案

我认为需要删除列中的 MultiIndex 使用 GroupBy.sizeGroupBy.count :

returnguidecount = (dfreturns.groupby(['issue_name','doc_type','doc_title'])
.size()
.reset_index(name='count'))
<小时/>
returnguidecount = (dfreturns.groupby(['issue_name','doc_type','doc_title'])['issue_name']
.count()
.reset_index(name='count'))
<小时/>
print (returnguidecount) 
issue_name doc_type doc_title \
0 The App keeps crashing / restarting / hanging GUIDE Fix the App
1 The App keeps crashing / restarting / hanging GUIDE Restart the device

count
0 2
1 4

区别在于 count 排除 groupby 之后指定的列中的 NaN 值。

关于python - 在 Python 中格式化聚合数据帧的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52496060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com