如何将重复的多个值附加到熊猫的列中

问题描述 投票:0回答:1

说我下面有pandas DataFrame:

       A      B      C   D
    0  foo    one    2   0
    1  foo    two    2   0
    2  foo    three  2   0
    4  bar    one    2   1
    5  bar    two    2   1
    6  bar    three  2   1

我想根据B的值在列'E'上附加值(0,1,2),然后对不同的'A'重复其自身预期输出:

       A      B      C   D  E
    0  foo    one    2   0  0
    1  foo    two    2   0  1
    2  foo    three  2   0  2
    4  bar    one    2   1  0
    5  bar    two    2   1  1
    6  bar    three  2   1  2

[我只知道如果我使用groupby()来获得E的值,但是如何使用常数来做到这一点?

python pandas jupyter-notebook append
1个回答
0
投票

用途:

df['E'] = df.groupby('A')['B'].transform(lambda x: pd.factorize(x)[0])
print (df)
     A      B  C  D  E
0  foo    one  2  0  0
1  foo    two  2  0  1
2  foo  three  2  0  2
4  bar    one  2  1  0
5  bar    two  2  1  1
6  bar  three  2  1  2

如果仅需要唯一值:

df['E'] = pd.factorize(df['B'])[0]
print (df)
     A      B  C  D  E
0  foo    one  2  0  0
1  foo    two  2  0  1
2  foo  three  2  0  2
4  bar    one  2  1  0
5  bar    two  2  1  1
6  bar  three  2  1  2
© www.soinside.com 2019 - 2024. All rights reserved.