pandas 将重复的值重新堆叠到列中

编程入门 行业动态 更新时间:2024-10-12 03:19:39
本文介绍了 pandas 将重复的值重新堆叠到列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

下面的DataFrame需要重新堆叠,以便我在一行上具有每个区域的所有值.在下面的示例中,新df仅包含3条线,每个区域一条.然后,相应的值将沿多列扩展.

The below DataFrame needs to be restacked, so that I have all values for each region on one line. In the below example the new df would only have 3 lines, one for each region. The corresponding values would then expand along multiple columns.

地区可能有所不同,可能会超过3个.任何建议,我们将不胜感激.

The regions may vary, and there may be more than 3. Any suggestions are appreciated.

>>> a Out[26]: Area value 0 EUROPE 47 1 ASIA 51 2 AMERICAS 37 3 EUROPE 39 4 ASIA 22 5 AMERICAS 24

所需的输出:

Europe 47 39 Asia 51 22 Americas 37 24

值应分布在不同的列中

推荐答案

您可以 groupby 在区域"和 apply list:

In [75]: df.groupby('Area')['value'].apply(list).reset_index() Out[75]: Area value 0 AMERICAS [37, 24] 1 ASIA [51, 22] 2 EUROPE [47, 39]

这将处理可变数量的值

如果要拆分值,可​​以调用apply并传递pd.Series ctor:

If you want to split the values out you can call apply and pass pd.Series ctor:

In [90]: df1 = df.groupby('Area')['value'].apply(lambda x: list(x)).reset_index() df1[['val1', 'val2']] = df1['value'].apply(pd.Series) df1 Out[90]: Area value val1 val2 0 AMERICAS [37, 24] 37 24 1 ASIA [51, 22] 51 22 2 EUROPE [47, 39] 47 39

编辑

对于可变数量的列,如果您不知道最大数量是多少,则不能预先分配,但仍可以使用上面的值:

For a variable number of columns you can't assign upfront if you don't know what the max number of values will be but you can still use the above:

In [94]: import io import pandas as pd t="""index Area value 0 EUROPE 47 1 ASIA 51 2 AMERICAS 37 3 EUROPE 39 4 ASIA 22 5 AMERICAS 24 5 AMERICAS 50""" df = pd.read_csv(io.StringIO(t), sep='\s+') df Out[94]: index Area value 0 0 EUROPE 47 1 1 ASIA 51 2 2 AMERICAS 37 3 3 EUROPE 39 4 4 ASIA 22 5 5 AMERICAS 24 6 5 AMERICAS 50 In [99]: df1 = df.groupby('Area')['value'].apply(list).reset_index() df1 Out[99]: Area value 0 AMERICAS [37, 24, 50] 1 ASIA [51, 22] 2 EUROPE [47, 39] In [102]: df1 = pd.concat([df1, df1['value'].apply(pd.Series).fillna(0)], axis=1) df1 Out[102]: Area value 0 1 2 0 AMERICAS [37, 24, 50] 37 24 50 1 ASIA [51, 22] 51 22 0 2 EUROPE [47, 39] 47 39 0

更多推荐

pandas 将重复的值重新堆叠到列中

本文发布于:2023-11-30 00:04:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:pandas   到列中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!