本文介绍了 pandas 数据框重塑/将多个值变量堆叠到单独的列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以某种方式重塑数据框.
Hi I'm trying to reshape a data frame in a certain way.
这是我拥有的数据框,
des1 des2 des3 interval1 interval2 interval3
value
aaa a b c ##1 ##2 ##3
bbb d e f ##4 ##5 ##6
ccc g h i ##7 ##8 ##9
des1对应于interval1,依此类推. interval列具有日期范围,而des列具有描述.
des1 corresponds with interval1 and so on. interval columns have a range of dates and des columns have descriptions.
我想重塑数据框,使其看起来像这样:
I'd like to reshape the dataframe such that it looks like this:
des interval
value
aaa a ##1
aaa b ##2
aaa c ##3
bbb d ##4
bbb e ##5
bbb f ##6
ccc g ##7
ccc h ##8
ccc i ##9
我将如何去做?我对.stack()有点熟悉,但是我一直无法得到我想要的.
How would I go about doing this? I'm a little familar with .stack() but I haven't been able to get exactly what I wanted.
感谢您的帮助.随时发布参考.
Thank you for your help. feel free to post references.
推荐答案
这可能是一种较短的方法:
This might be a shorter approach:
[72]:
df.columns = pd.MultiIndex.from_tuples(map(lambda x: (x[:-1], x), df.columns))
In [73]:
print pd.DataFrame({key:df[key].stack().values for key in set(df.columns.get_level_values(0))},
index = df['des'].stack().index.get_level_values(0))
des interval
value
aaa a ##1
aaa b ##2
aaa c ##3
bbb d ##4
bbb e ##5
bbb f ##6
ccc g ##7
ccc h ##8
ccc i ##9
或保留1,2,3信息:
Or preserve the 1,2,3 info:
[73]:
df.columns = pd.MultiIndex.from_tuples(map(lambda x: (x[:-1], x[-1]), df.columns))
Keys = set(df.columns.get_level_values(0))
df2 = pd.concat([df[key].stack() for key in Keys], axis=1)
df2.columns = Keys
print df2
des interval
value
aaa 1 a ##1
2 b ##2
3 c ##3
bbb 1 d ##4
2 e ##5
3 f ##6
ccc 1 g ##7
2 h ##8
3 i ##9
这篇关于 pandas 数据框重塑/将多个值变量堆叠到单独的列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!