如果满足条件熊猫数据框,则替换字符串

时间:2020-07-20 09:40:18

标签: pandas string dataframe rename

我有以下代码,如果字符串或数字中有括号,我们想替换国家名称,我们必须将其删除 例如 “玻利维亚(多民族国)”应为“玻利维亚”, “ Switzerland17”应为“ Switzerland”。

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
pd.set_option('display.max_columns',None)
pd.set_option('display.max_rows',None)

df=pd.read_excel('Energy Indicators.xls',skiprows=17, skipfooter=265-(227))
df.drop(df.columns[[0,1]], axis=1, inplace=True)
df.columns=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']

1 个答案:

答案 0 :(得分:1)

尝试使用pd.DataFrame.str.replace

df=pd.DataFrame({'Country':['Bolivia (Plurinational State of)','Switzerland17'],'value':[1,2]})
df
#   Country                           value
#0  Bolivia (Plurinational State of)    1
#1  Switzerland17                       2

df.Country=df.Country.str.replace('\(.*\)|(\d+)','')
df
#   Country                           value
#0  Bolivia                             1
#1  Switzerland                         2