在不使用.loc的情况下根据条件更新列

时间:2016-11-18 13:13:54

标签: pandas dataframe

我正在尝试根据条件更新列的值。但是,它仅在我使用[1, 4, 7, 11, 44, 111, 444, 777, 12] [2, 5, 8, 22, 55, 222, 555, 888, 13] [3, 6, 9, 33, 66, 333, 666, 999, 14] 0.0000650 [1, 4, 7, 11, 44, 111, 444, 777, 12] [2, 5, 8, 22, 55, 222, 555, 888, 13] [3, 6, 9, 33, 66, 333, 666, 999, 14] 0.0000356 [1, 4, 7, 11, 44, 111, 444, 777, 12] [2, 5, 8, 22, 55, 222, 555, 888, 13] [3, 6, 9, 33, 66, 333, 666, 999, 14] 0.0002259 时有效。我想知道我是否可以在不使用.loc的情况下完成。

这是没有.loc的代码(我没有收到错误,但是没有更新值):

.loc

以下是.loc正常工作的代码:

mask1 = GDP['Country Name'] == "Korea, Rep."
GDP[mask1]['Country Name'] = "South Korea"

谢谢!

1 个答案:

答案 0 :(得分:1)

使用replace

df['Country Name'] = df['Country Name'].replace({'Korea, Rep.':'South Korea'})

<强>计时

In [220]: %timeit (rep(GDP))
100 loops, best of 3: 2.79 ms per loop

In [221]: %timeit (orig(GDP1))
100 loops, best of 3: 3.31 ms per loop

测试代码

import pandas as pd

GDP = pd.DataFrame({'Country Name':['Korea, Rep.','aa','ss']})
#[30000 rows x 1 columns]
GDP = pd.concat([GDP]*10000).reset_index(drop=True)
#print (GDP)

GDP1 = GDP.copy()

def rep(GDP):
    GDP['Country Name'] = GDP['Country Name'].replace({'Korea, Rep.':'South Korea'})
    return (GDP)

def orig(GDP):
    GDP.loc[GDP['Country Name'] == "Korea, Rep.", 'Country Name'] = "South Korea"
    return (GDP)


print (rep(GDP))    
print (orig(GDP1))  
相关问题