pandas 逗号分隔层次结构 groupby 总和
pandas comma separated hierarchy groupby sum
我有以下分层数据的数据格式。单个级别和可变深度可以有多个行。我试图得到一个结果,在 col_2 中我们看到实例的所有低级别的总和。
使用简单的 groupby 不起作用,因为它不理解层次结构。我尝试将 col_1 拆分为多个列,命名为 level-1 到 level-6(深度),因此 groupby level-1 到 level-6,但尽管数据帧是多索引,结果仍然不正确。
分离前代码:
df.groupby(["col_1"], as_index=False).sum()
分离后的代码:
df.groupby(["level-1","level-2","level-3","level-4","level-5","level-6"], as_index=False).sum()
如有任何帮助,我们将不胜感激!
更新感谢@Yo_Chris到目前为止:
import pandas as pd
# sample data
df = pd.DataFrame({'Col1': ['PUU', 'PUU;UT', 'PUU;UT', 'PUU;UT;AHU', 'PUU;UT;AHU;CSP', 'PUU;AS', 'PUU;PREV', 'PUU;TECHNOLOGY', 'PUU;TECHNOLOGY', 'PUU;TECHNOLOGY;SPEC'],
'Col2': [1000,1000,50,500,250,100,1000,300,500,900]})
# groupby, sum and invert
s = df.groupby('Col1')['Col2'].sum()[::-1]
# groupby, cumsum and invert
s.groupby(s.index.str[0]).cumsum()[::-1])```
# this results in the following:
Col1
PUU 5600
PUU;AS 4600
PUU;PREV 4500
PUU;TECHNOLOGY 3500
PUU;TECHNOLOGY;SPEC 2700
PUU;UT 1800
PUU;UT;AHU 750
PUU;UT;AHU;CSP 250
Name: Col2, dtype: int64
而我们想要的是:
PUU 5600
PUU;AS 100
PUU;PREV 1000
PUU;TECHNOLOGY 1700
PUU;TECHNOLOGY;SPEC 900
PUU;UT 1800
PUU;UT;AHU 750
PUU;UT;AHU;CSP 250
我根据你的示例数据做了一些假设。 col1 总是由分号分隔的单个字符,并且 col1 总是排序的:col1 不能是 ['a;b;c', 'a', 'a;b'...]
# sample data
df = pd.DataFrame({'Col1': ['a', 'a;b', 'a;b', 'a;b;c', 'a;b;c;d', 'e', 'f', 'g', 'g', 'g;h'],
'Col2': [1000,1000,50,500,250,100,1000,300,500,900]})
# groupby, sum and invert
s = df.groupby('Col1')['Col2'].sum()[::-1]
# groupby, cumsum and invert
s.groupby(s.index.str[0]).cumsum()[::-1]
# return a pd.Series
Col1
a 2800
a;b 1800
a;b;c 750
a;b;c;d 250
e 100
f 1000
g 1700
g;h 900
Name: Col2, dtype: int64
最终通过拆分 col_1 以按深度分隔列来解决此问题。然后按每列(深度 1、2、..6)分组并连接所有结果数据帧。不是很干净,但可以正常工作!
我有以下分层数据的数据格式。单个级别和可变深度可以有多个行。我试图得到一个结果,在 col_2 中我们看到实例的所有低级别的总和。
使用简单的 groupby 不起作用,因为它不理解层次结构。我尝试将 col_1 拆分为多个列,命名为 level-1 到 level-6(深度),因此 groupby level-1 到 level-6,但尽管数据帧是多索引,结果仍然不正确。
分离前代码:
df.groupby(["col_1"], as_index=False).sum()
分离后的代码:
df.groupby(["level-1","level-2","level-3","level-4","level-5","level-6"], as_index=False).sum()
如有任何帮助,我们将不胜感激!
更新感谢@Yo_Chris到目前为止:
import pandas as pd
# sample data
df = pd.DataFrame({'Col1': ['PUU', 'PUU;UT', 'PUU;UT', 'PUU;UT;AHU', 'PUU;UT;AHU;CSP', 'PUU;AS', 'PUU;PREV', 'PUU;TECHNOLOGY', 'PUU;TECHNOLOGY', 'PUU;TECHNOLOGY;SPEC'],
'Col2': [1000,1000,50,500,250,100,1000,300,500,900]})
# groupby, sum and invert
s = df.groupby('Col1')['Col2'].sum()[::-1]
# groupby, cumsum and invert
s.groupby(s.index.str[0]).cumsum()[::-1])```
# this results in the following:
Col1
PUU 5600
PUU;AS 4600
PUU;PREV 4500
PUU;TECHNOLOGY 3500
PUU;TECHNOLOGY;SPEC 2700
PUU;UT 1800
PUU;UT;AHU 750
PUU;UT;AHU;CSP 250
Name: Col2, dtype: int64
而我们想要的是:
PUU 5600
PUU;AS 100
PUU;PREV 1000
PUU;TECHNOLOGY 1700
PUU;TECHNOLOGY;SPEC 900
PUU;UT 1800
PUU;UT;AHU 750
PUU;UT;AHU;CSP 250
我根据你的示例数据做了一些假设。 col1 总是由分号分隔的单个字符,并且 col1 总是排序的:col1 不能是 ['a;b;c', 'a', 'a;b'...]
# sample data
df = pd.DataFrame({'Col1': ['a', 'a;b', 'a;b', 'a;b;c', 'a;b;c;d', 'e', 'f', 'g', 'g', 'g;h'],
'Col2': [1000,1000,50,500,250,100,1000,300,500,900]})
# groupby, sum and invert
s = df.groupby('Col1')['Col2'].sum()[::-1]
# groupby, cumsum and invert
s.groupby(s.index.str[0]).cumsum()[::-1]
# return a pd.Series
Col1
a 2800
a;b 1800
a;b;c 750
a;b;c;d 250
e 100
f 1000
g 1700
g;h 900
Name: Col2, dtype: int64
最终通过拆分 col_1 以按深度分隔列来解决此问题。然后按每列(深度 1、2、..6)分组并连接所有结果数据帧。不是很干净,但可以正常工作!