Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从数据帧中删除子字符串_Python_Pandas - Fatal编程技术网

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

大家好,我是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'\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