使用Pandas转换为长面板数据格式 [英] Converting to long panel data format with pandas
问题描述
我有一个DataFrame,其中的行代表时间,而列则代表个人.我想以一种有效的方式将其转换为大熊猫的长面板数据格式,因为DataFame相当大.我想避免循环.这是一个示例:以下DataFrame:
I have a DataFrame where rows represent time and columns represent individuals. I want to turn it into into long panel data format in pandas in an efficient manner, as the DataFames are rather large. I would like to avoid looping. Here is an example: The following DataFrame:
id 1 2
date
20150520 3.0 4.0
20150521 5.0 6.0
应转换为:
date id value
20150520 1 3.0
20150520 2 4.0
20150520 1 5.0
20150520 2 6.0
由于数据大小,速度对我而言真正重要.如果要权衡,我宁愿选择它而不是优雅.尽管我怀疑我可能缺少一个相当简单的功能,但熊猫应该能够处理该功能.有什么建议吗?
Speed is what's really important to me, due to the data size. I prefer it over elegance if there is a tradeoff. Although I suspect I mam missing a rather simple function, pandas should be able to handle that. Any suggestions?
推荐答案
我认为您需要 重置索引
:
I think you need stack
with reset_index
:
print (df)
1 2
date
20150520 3.0 4.0
20150521 5.0 6.0
df = df.stack().reset_index()
df.columns = ['date','id','value']
print (df)
date id value
0 20150520 1 3.0
1 20150520 2 4.0
2 20150521 1 5.0
3 20150521 2 6.0
print (df)
id 1 2
date
20150520 3.0 4.0
20150521 5.0 6.0
df = df.stack().reset_index(name='value')
print (df)
date id value
0 20150520 1 3.0
1 20150520 2 4.0
2 20150521 1 5.0
3 20150521 2 6.0
这篇关于使用Pandas转换为长面板数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!