Python 从数据帧中删除子字符串
大家好,我是Python新手,我想从数据帧中的行中删除一些字符。问题是我有几个国家,所有这些国家的括号中的信息都不同,所以我尝试了替换和一些通配符,但根本不起作用Python 从数据帧中删除子字符串,python,pandas,Python,Pandas,大家好,我是Python新手,我想从数据帧中的行中删除一些字符。问题是我有几个国家,所有这些国家的括号中的信息都不同,所以我尝试了替换和一些通配符,但根本不起作用 第1列 国家(其他信息) 我只想得到: 第1列 国家/地区选项1 在第1列中替换 df['Column 1'].str.replace(r'\s*\(.*\)', '') 0 Country Name: Column 1, dtype: object 选项2 获取整个df df.stack().str.replace(r
第1列
国家(其他信息) 我只想得到:
第1列
国家/地区选项1 在
第1列中替换
df['Column 1'].str.replace(r'\s*\(.*\)', '')
0 Country
Name: Column 1, dtype: object
选项2
获取整个df
df.stack().str.replace(r'\s*\(.*\)', '').unstack()
选项1
在第1列中替换
df['Column 1'].str.replace(r'\s*\(.*\)', '')
0 Country
Name: Column 1, dtype: object
选项2
获取整个df
df.stack().str.replace(r'\s*\(.*\)', '').unstack()
另一种使用方法的解决方案:
DF:
解决方案:
In [30]: df.Column1.str.split(r'\s*\(').str[0]
Out[30]:
0 Country
1 Yet another country
Name: Column1, dtype: object
另一种使用方法的解决方案:
DF:
解决方案:
In [30]: df.Column1.str.split(r'\s*\(').str[0]
Out[30]:
0 Country
1 Yet another country
Name: Column1, dtype: object