根据列值替换熊猫数据框的一部分

时间:2019-08-29 17:37:05

标签: python-3.x pandas dataframe anaconda

我有一个看起来像这样的Pandas Dataframe:

import numpy as np
import pandas as pd

# Create data
name  = np.array([0,0,0,1,1,1,2,2,2])
data  = np.random.rand(9)
label = ['squirrel']*9

df = pd.DataFrame({'Name': name,
                   'Data': data,
                  'label': label})

"""
Produces:
    Name      Data     label
0      0  0.518886  squirrel
1      0  0.167908  squirrel
2      0  0.918280  squirrel
3      1  0.027823  squirrel
4      1  0.392099  squirrel
5      1  0.724943  squirrel
6      2  0.480646  squirrel
7      2  0.737251  squirrel
8      2  0.212135  squirrel
"""

我还有另一个长度为3的数组,例如new_array = 10*np.ones(3)。对于Data所在的行,如何用新数组替换Name = 1列中的值?理想的结果是:

    Name      Data     label
0      0  0.518886  squirrel
1      0  0.167908  squirrel
2      0  0.918280  squirrel
3      1  10.00000  squirrel
4      1  10.00000  squirrel
5      1  10.00000  squirrel
6      2  0.480646  squirrel
7      2  0.737251  squirrel
8      2  0.212135  squirrel

我尝试隔离数据框Name = 1,并将这些值设置为等于新数组:

df.loc[df['Name'] == '1']['Data'] = new_array

此外,我还研究了replace,并研究了.loc.iloc.ix

1 个答案:

答案 0 :(得分:2)

您可以使用:

df.loc[df['Name']==1,['Data']]=new_array

输出:

   Name Data        label
0   0   0.587762    squirrel
1   0   0.612776    squirrel
2   0   0.098259    squirrel
3   1   10.000000   squirrel
4   1   10.000000   squirrel
5   1   10.000000   squirrel
6   2   0.180574    squirrel
7   2   0.782238    squirrel
8   2   0.777101    squirrel

说明:

使用locData的{​​{1}}列中选择其中的值,其中rows

注意:

请记住,其余的值与您的示例不同,因为它们是随机生成的数字。