[英]Using loc to replace values gives error
My code looks like: 我的代码如下:
import pandas as pd
df = pd.read_excel("Energy Indicators.xls", header=None, footer=None)
c_df = df.copy()
c_df = c_df.iloc[18:245, 2:]
c_df = c_df.rename(columns={2: 'Country', 3: 'Energy Supply', 4:'Energy Supply per Capita', 5:'% Renewable'})
c_df['Energy Supply'] = c_df['Energy Supply'].apply(lambda x: x*1000000)
print(c_df)
c_df = c_df.loc[c_df['Country'] == ('Korea, Rep.')] = 'South Korea'
When I run it, I get the error "'str' has no attribute 'loc'". 当我运行它时,出现错误“ str”没有属性“ loc””。 It seems like it is telling me that I can't use loc on the dataframe.
似乎是在告诉我无法在数据帧上使用loc。 All I want to do is replace the value so if there is an easier way, I am all ears.
我要做的就是替换值,因此,如果有更简单的方法,我将不知所措。
I would suggest using df.replace
: 我建议使用
df.replace
:
df = df.replace({'c_df':{'Korea, Rep.':'South Korea'}})
The code above replaces Korea, Rep.
with South Korea
only in the column c_df
. 上面的代码仅在
c_df
列c_df
South Korea
替换了Korea, Rep.
。 Take a look at the df.replace
documentation , which explains the nested dictionary syntax I used above as : 看一下
df.replace
文档 ,该文档解释了我上面使用的嵌套字典语法:
Nested dictionaries, eg, {'a': {'b': nan}}, are read as follows: look in column 'a' for the value 'b' and replace it with nan.
嵌套字典,例如{'a':{'b':nan}}的读取方式如下:在'a'列中查找值'b'并将其替换为nan。 You can nest regular expressions as well.
您也可以嵌套正则表达式。 Note that column names (the top-level dictionary keys in a nested dictionary) cannot be regular expressions.
请注意,列名(嵌套字典中的顶级字典键)不能为正则表达式。
Example : 范例 :
# Original dataframe:
>>> df
c_df whatever
0 Korea, Rep. abcd
1 x abcd
2 Korea, Rep. abcd
3 y abcd
# After df.replace:
>>> df
c_df whatever
0 South Korea abcd
1 x abcd
2 South Korea abcd
3 y abcd
Just do 做就是了
c_df.loc[c_df['Country'] == ('Korea, Rep.')] = 'South Korea'
instead of 代替
c_df = c_df.loc[c_df['Country'] == ('Korea, Rep.')] = 'South Korea'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.