我是熊猫新手。我有一个赞助商和公司参加考试的数据框:
import pandas pd
df = pd.DataFrame({
'sponsor': ['A71991', 'A71991', 'A71991', 'A81001', 'A81001'],
'sponsor_class': ['Industry', 'Industry', 'Industry', 'NIH', 'NIH'],
'year': [2012, 2013, 2013, 2012, 2013],
'passed': [True, False, True, True, True],
})
现在,我要输出一个CSV文件,其中包含每个赞助商及其类的行,以及按年度列出的通过率和总费率的列:
^{pr2}$如何从df
到这个经过重构的数据帧?我想我需要按sponsor
和sponsor_class
分组,然后将总数和{pd.write_csv(mydf)
结尾。)
我试着从这个开始:
df_g = df.groupby(['sponsor', 'sponsor_class', 'year', 'passed'])
但这给了我一个空的数据帧。在
我想我需要一个透视表来透视这一年并通过状态。。。但我不知道从哪里开始。在
更新:获取某个地方:
df_g = df_completed.pivot_table(index=['lead_sponsor', 'lead_sponsor_class'],
columns='year',
aggfunc=len, fill_value=0)
df_g[['passed']]
现在我需要解决(1)如何获得所有行的计数以及passed
,以及(2)如何为CSV文件取消列嵌套。在
我可以通过几个步骤了解如何做到这一点:
结果:
^{pr2}$最后:
相关问题 更多 >
编程相关推荐