这是一个数据框的示例,
id Section A B
0 abc foo 0.1 0.6
1 abc foo 0.2 0.3
2 abc bar 0.5 0.1
3 def foo 0.1 0.1
4 def bar 0.1 0.3
5 def bar 0.6 0.1
6 ghj foo 0.3 0.1
7 ghj foo 0.1 0.7
8 ghj bar 0.1 0.2
df['AA', 'BB']
从以下列表中创建新列。
A_foo = [0.1,2]
A_bar = [1,0.3]
B_foo = [0.4,0.2]
B_bar = [1.2,0.5]
到目前为止,这是我尝试过的方法
g = df.groupby('id')['A','B']
for i, i_d in g:
print(i_d)
**
length of `A_foo, A_bar, B_foo and B_bar` is always greater or equal to df`
[df.Section == 'foo'] and df[df.Section == 'bar']` of any unique id.
然后创建df['AA']
,对于每个'foo' and 'bar'
在df['Section']
每个ID,我想从采取相应的值A_foo and A_bar
。
例如,在第一个i_d(id = abc)中,将df.A
具有two 'foo' and one 'bar'
的前三行df['AA']
,
[0.1,2,1... 0.1 and 2 from A_foo and 1 from A_bar
然后在第二个i_d(id='def')
,df.A has one foo and two bar
所以我需要添加0.1 from A_foo and 1,0.3 from A_bar
。现在
df['AA'] will look [0.1,2,1,0.1,1,0.3...
从上一个i_d开始,我0.1,2 from A_foo and 1 from A_bar.
现在将收集完整的
df['AA'] = [0.1,2,1,0.1,1,0.3,0.1,2,1]
同样,df['BB']
从B_foo
和创建B_bar
df['BB'] = [0.4,0.2,1.2,0.4,1.2,0.5,0.4,0.2,1.2]
这是最终的df
id Section A B AA BB
0 abc foo 0.1 0.6 0.1 0.4
1 abc foo 0.2 0.3 2.0 0.2
2 abc bar 0.5 0.1 1.0 1.2
3 def foo 0.1 0.1 0.1 0.4
4 def bar 0.1 0.3 1.0 1.2
5 def bar 0.6 0.1 0.3 0.5
6 ghj foo 0.3 0.1 0.1 0.4
7 ghj foo 0.1 0.7 2.0 0.2
8 ghj bar 0.1 0.2 1.0 1.2
用groupby
+创建索引cumcount
,然后使用np.select
来分配各个列表中的值。
import numpy as np
df['idx'] = df.groupby(['id', 'Section']).cumcount()
conds = [df.Section.eq('foo'), df.Section.eq('bar')]
AA_choice = [np.array(A_foo)[df.idx], np.array(A_bar)[df.idx]]
BB_choice = [np.array(B_foo)[df.idx], np.array(B_bar)[df.idx]]
df['AA'] = np.select(conds, AA_choice, default=np.NaN)
df['BB'] = np.select(conds, BB_choice, default=np.NaN)
id Section A B idx AA BB
0 abc foo 0.1 0.6 0 0.1 0.4
1 abc foo 0.2 0.3 1 2.0 0.2
2 abc bar 0.5 0.1 0 1.0 1.2
3 def foo 0.1 0.1 0 0.1 0.4
4 def bar 0.1 0.3 0 1.0 1.2
5 def bar 0.6 0.1 1 0.3 0.5
6 ghj foo 0.3 0.1 0 0.1 0.4
7 ghj foo 0.1 0.7 1 2.0 0.2
8 ghj bar 0.1 0.2 0 1.0 1.2
如果您的清单不够长,则会显示IndexError
。如果是这样,则可以考虑通过以下方式进行切片:np.array(A_foo)[df.idx%len(A_foo)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句