简体   繁体   English

使用Pandas到CSV,如何在多级索引中组织时间和数值数据

[英]Using pandas to csv, how to organize time and numerical data in a multi-level index

Using pandas to write to a csv, I want Monthly Income sums for each unique Source. 我想用熊猫写一个csv,我希望每个唯一来源的每月收入总和。 Month is in datetime format. 月采用日期时间格式。

I have tried resampling and groupby methods, but groupby neglects month and resampling neglects source. 我尝试过重采样和groupby方法,但是groupby忽略了月份,而重采样则忽略了源代码。 I currently have a multi-level index with Month and Source as indexes. 我目前有一个以月和源作为索引的多级索引。

   Month        Source   Income
2019-03-01        A        100
2019-03-05        B        50
2019-03-06        A        4
2019-03-22        C        60
2019-04-23        A        40
2019-04-24        A        100
2019-04-24        C        30
2019-06-1         C        100
2019-06-1         B        90
2019-06-8         B        20
2019-06-12        A        50
2019-06-27        C        50

I can groupby Source which neglects date, or I can resample for date which neglects source. 我可以对忽略日期的Source进行分组,也可以对忽略Source的日期进行重新采样。 I want monthly sums for each unique source. 我想要每个唯一来源的每月金额。

What you have in the Month column is a Timestamp . 您在“ 月份”列中拥有的是时间戳记 So you can separate the month attribute of this Timestamp and afterward apply the groupby method, like this: 因此,您可以分离此时间戳记的month属性,然后再应用groupby方法,如下所示:

df.columns = ['Timestamp', 'Source', 'Income']

month_list = []
for i in range(len(df)):
    month_list.append(df.loc[i,'Timestamp'].month)

df['Month'] = month_list

df1 = df.groupby(['Month', 'Source']).sum()

The output should be like this: 输出应如下所示:

               Income
Month   Source  
3          A    104
           B    50
           C    60
4          A    140
           C    30
6          A    50
           B    110
           C    150

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

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