![](/img/trans.png)
[英]Converting list to comma separated integers to be substituted in IN clause of Pandas dataframe query
[英]Pandas: Comma Separated Excel Cells not Converting to List
我加入了 3 个 Excel 选项卡数据集以提供我的基本数据框,然后对于每一行,我想计算 DUAlloc 中逗号分隔值中的 int 值,然后将 Amount 除以 DUAlloc 计数,然后遍历 DuAlloc 列表并分配单独的行,例如
基础数据:
描述 | DuAlloc | 数量 |
---|---|---|
废话 | 1,2,3,4,5 | 1000 |
雅达 | 30,15,3,4,5 | 200 |
处理数据:
描述 | DuAlloc | 数量 |
---|---|---|
废话 | 1 | 200 |
废话 | 2 | 200 |
废话 | 3 | 200 |
雅达 | 3 | 40 |
废话 | 4 | 200 |
雅达 | 4 | 40 |
废话 | 5 | 200 |
雅达 | 5 | 40 |
雅达 | 15 | 40 |
雅达 | 30 | 40 |
我尝试了多种转换为列表的方法:list()、tolist(),但要么对所有计数获得相同的数字,要么我最接近的是[len(str(c)) for c in df3['DUAlloc']]
计算所有我不想要的字符。
我将如何实现这一目标,熊猫是最好的选择吗?
使用Series.str.split
、 df.explode
、 Groupby.transform
和df.div
:
In [501]: out = df.assign(DuAlloc=df['DuAlloc'].str.split(',')).explode('DuAlloc')
In [506]: out['Amount'] = out['Amount'].div(out.groupby('Description')['Amount'].transform('size'))
In [507]: out
Out[507]:
Description DuAlloc Amount
0 Blah 1 200.0
0 Blah 2 200.0
0 Blah 3 200.0
0 Blah 4 200.0
0 Blah 5 200.0
1 Yada 30 40.0
1 Yada 15 40.0
1 Yada 3 40.0
1 Yada 4 40.0
1 Yada 5 40.0
您可以使用.str.count
来计算,
列的数量。
out = (df.assign(Amount=df['Amount'].div(df['DuAlloc'].str.count(',').add(1)),
DuAlloc=df['DuAlloc'].str.split(','))
.explode('DuAlloc'))
print(out)
Description DuAlloc Amount
0 Blah 1 200.0
0 Blah 2 200.0
0 Blah 3 200.0
0 Blah 4 200.0
0 Blah 5 200.0
1 Yada 30 40.0
1 Yada 15 40.0
1 Yada 3 40.0
1 Yada 4 40.0
1 Yada 5 40.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.