示例数据帧

CountryName

India|Pakistan
Pakistan|Agansitan
Sweden
Nepal|Bhutan

Output dataframe witha new column
CountryName           MainCountry

India|Pakistan        India
Pakistan|Agansitan    Pakistan
Sweden                Sweden
Nepal|Bhutan          Nepal

I tried like
df["MainCountry"] =df['CountryName'].str.contains("[|].*","")

its giving true or false , can you help me in finding out how to get that

最佳答案

使用str.extract

df.assign(MainCountry=df.CountryName.str.extract(r'(.*?)(?:\||$)'))

          CountryName MainCountry
0      India|Pakistan       India
1  Pakistan|Agansitan    Pakistan
2              Sweden      Sweden
3        Nepal|Bhutan       Nepal

str.partition
df.assign(MainCountry=df.CountryName.str.partition('|')[0])

          CountryName MainCountry
0      India|Pakistan       India
1  Pakistan|Agansitan    Pakistan
2              Sweden      Sweden
3        Nepal|Bhutan       Nepal

10-01 13:20