python - Pandas 相对时间轴

标签 python pandas csv dataframe pivot

我有过去八个月的客户数据,但是这几个月不是同一个月,只是他们恰好在我们这里的最后几个月。月费和罚款存储在行中,但我希望过去八个月中的每一个月都是一列。

我有什么:

Customer Amount Penalties Month
123      500    200       1/7/2017
123      400    100       1/6/2017
   ...
213      300    150       1/4/2015
213      200    400       1/3/2015

我想要的:

Customer Month-8-Amount Month-7-Amount ... Month-1-Amount Month-1-Penalties ...
123      500            400                450            300
213      900            250                300            200
...

我尝试过的:

df = df.pivot(index=num, columns=[amount,penalties])

我遇到了这个错误:

ValueError: all arrays must be same length

有什么理想的方法可以做到这一点吗?

最佳答案

您可以使用 unstackset_index

# assuming all date is sort properly , then we do cumcount
df['Month']=df.groupby('Customer').cumcount()+1 

# slice the most recent 8 one 
df=df.loc[df.Month<=8,:]# slice the most recent 8 one 

# doing unstack to reshape your df 
s=df.set_index(['Customer','Month']).unstack().sort_index(level=1,axis=1)

# flatten multiple index to one 
s.columns=s.columns.map('{0[0]}-{0[1]}'.format) 
s.add_prefix("Month-")
Out[189]: 
          Month-Amount-1  Month-Penalties-1  Month-Amount-2  Month-Penalties-2
Customer                                                                      
123                  500                200             400                100
213                  300                150             200                400

关于python - Pandas 相对时间轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50136563/

相关文章:

python - Python 日志记录模块中字段组合的固定长度格式

python - 在 Python 中将两个列表合并为一个

python-3.x - Python 中的重采样

python - 如何从每个键有多个值的字典中写入 csv 文件?

file - 读取/写入/查找/替换巨大的csv文件

python - 如何在 Python 中查找并替换每行特定字符后面的文本?

python - openpyxl 覆盖所有数据而不是更新 excel 表

python - 有没有办法在没有目标标签的情况下对文本进行分类?

python - 从 Pandas/Python 中选定的单元格访问索引/行/列

python - 在 Pandas 数据框中查找列和索引