How can I resample (upsample) my Pandas Dataframe?

扶醉桌前 提交于 2021-01-28 14:32:27

问题


I have a small Pandas DataFrame I'd like to resample, and I hoped you could help me :)

I cannot show it to you as it is confidential but I can describe to you a simpler version of it.

It has 4 columns:

-Date (YYYY-MM-DD format)

-Country

-Amount

-Frequency

So here is what I'd like to do:

Depending on the rows, the Frequency is either YEARLY or MONTHLY If it happens to be monthly, then nothing needs to be done.

But if it's YEARLY, I would like to change it to MONTHLY and insert 11 new rows so that in the 12 rows (the existing one and the 11 new ones) considered, the country would remain the same (so same country name repeted 12 times), the amount would be amount/12 (repeted 12 times too, you get it), and the date would be incremented by 1 month at each row.

I really wish someone could help me with that!

Thanks in advance,

Alex

EDIT:

I already change the Date Column as an index using

df.set_index("Date",inplace=True)

However, I don't really know how to use the resample function for my purpose...


回答1:


First ensure that your dataframe has an index of type DateTimeIndex.

Then use the resample function to either upsample (higher frequency) or downsample (lower frequency) your dataframe. Then apply an aggregator (e.g. sum) to aggregate the values across the new sampling frequency.

See the resample documentation here: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html



来源:https://stackoverflow.com/questions/53265779/how-can-i-resample-upsample-my-pandas-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!