【问题标题】:Merging DataFrame by an amount of rows [duplicate]按行数合并DataFrame [重复]
【发布时间】:2020-04-28 14:09:14
【问题描述】:

假设我有这种数据框:

            Count
Trip Date        
2014-01-01   5370
2014-01-02   8374
2014-01-03   1121
2014-01-04   2246
2014-01-05   2626
...           ...
2019-11-26  63410
2019-11-27  51121
2019-11-28  16090
2019-11-29  23095
2019-11-30  26389

我怎样才能得到一个看起来像这样的新 df:

             Count
Week number        
1             C1
2             C2
3             C3
4             C4
5             C4
...           ...
n-1         C(n-1)
n             Cn

Ci 是每 7 天 Count 的总和?

【问题讨论】:

  • df.resample('w').sum()

标签: python pandas dataframe


【解决方案1】:

首先,您需要将索引设置为日期。

df.set_index(df["date"],inplace=True)

然后你只需像这样每周或每月做一天

df.resample('D').sum()
df.resample('M').sum()
df.resample('Y').sum()

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html

【讨论】:

  • 谢谢!问题是,当我以这种方式执行此操作时,什么也没有发生,但是当我输入 df = df['count']... 时,我得到一个系列作为输出,没有列 count
  • dtype 是那种 col 的某种 int 吗?
  • 看来我是在结合新旧语法。没有 ['count'] 的 df 怎么办
  • 我的意思是如何在重采样后再次获取数据帧?
  • 现在可以完美运行了。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-11-05
  • 2016-03-15
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多