[英]Finding an efficient way to reshape a dataframe: from n*m to 1*(n+m)
I'm finding an efficient to reshape a N*M dataframe to 1*(N*M) dataframe:我找到了一种将 N*M 数据帧重塑为 1*(N*M) 数据帧的有效方法:
INPUT>输入>
df1
ID distUnit col_a col_b
1000 150 35 55
1000 250 10 20
1200 150 12 13
1200 250 16 20
DESIRED OUTPUT>期望输出>
ID col_a_150 col_b_150 col_a_250 col_b_250
1000 35 55 10 20
1200 12 13 16 20
My idea>我的想法>
row['distUnit']
根据row['distUnit']
的值为col_a和col_b添加前缀combined_first
to add processed row back to result dataframe使用combined_first
添加处理行回导致数据帧Challenging part >挑战部分 >
Since the size of my input data is 14440 * 20, my idea is not efficient enough.由于我输入数据的大小是14440 * 20,所以我的想法不够高效。
Wondering any better implementation ways to solve this?想知道有什么更好的实现方法来解决这个问题吗?
Thanks for reading.谢谢阅读。
If pair (ID, distUnit) is unique across your dataset, you can simply "unmelt" your dataframe like this:如果pair (ID, distUnit) 在您的数据集中是唯一的,您可以像这样简单地“解开”您的数据框:
df=df.groupby(['ID','distUnit'])['col_a','col_b'].mean().unstack()
df.columns = [f'{col[0]}_{col[1]}' for col in df.columns.values]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.