简体   繁体   中英

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'". It seems like it is telling me that I can't use loc on the dataframe. 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 = df.replace({'c_df':{'Korea, Rep.':'South Korea'}})

The code above replaces Korea, Rep. with South Korea only in the column c_df . Take a look at the df.replace documentation , which explains the nested dictionary syntax I used above as :

Nested dictionaries, eg, {'a': {'b': nan}}, are read as follows: look in column 'a' for the value 'b' and replace it with 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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM