Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 使用Pandas从数据帧中删除部分字符串_Python_String_Pandas_Series - Fatal编程技术网

Python 使用Pandas从数据帧中删除部分字符串

Python 使用Pandas从数据帧中删除部分字符串,python,string,pandas,series,Python,String,Pandas,Series,如果我有这样一个数据帧: id str 01 abc_d(a) 02 ab_d(a) 03 abcd_e(a) 04 a_b(a) 如何获得如下所示的数据帧?对不起,我用这个数据框来表示我真正的问题。谢谢 id str 01 d 02 d 03 e 04 b 使用提取 df['str']=df['str'].str.extract("\_(.*)\(",expand=True) df Out[585]: id str 0

如果我有这样一个数据帧:

id    str
01    abc_d(a)
02    ab_d(a)
03    abcd_e(a)
04    a_b(a)
如何获得如下所示的数据帧?对不起,我用这个数据框来表示我真正的问题。谢谢

id    str
01    d
02    d
03    e
04    b

使用
提取

df['str']=df['str'].str.extract("\_(.*)\(",expand=True) 
df
Out[585]: 
   id str
0   1   d
1   2   d
2   3   e
3   4   b

可能您可以尝试
split
类似于:

或者

使用。特定于您的特定格式

df['str'] = df['str'].str.split('_').str[-1].str[0]

print(df)

   id str
0   1   d
1   2   d
2   3   e
3   4   b
(回答不好)

Series.str.split
soup
(回答不那么糟糕)

Series.str.extract
Regex方法带来了相当大的开销,而
str.extract
并不能使事情变得更好


(更好的答案)

使用列表组件重新搜索
这结合了两个方面的优点,列表comp的性能和纯python字符串拆分的速度。应该是最快的


性能

df_test = pd.concat([df] * 10000, ignore_index=True)


又冷了!如果你有时间,你能简单地解释一下,如果答案表达的是相同的结果,那么答案是好的、不好的还是更好的?我是一个初学者,在质量方面看不出有什么直接的区别,但你最好的答案在可读性方面对我来说似乎是最差的(无意冒犯)@sudonym是的,给我一分钟,我再补充一些信息;-)和往常一样,我印象深刻-祝你一天愉快!我不知道你为什么选择
str.split('(').str[0].str.split(''''').str[-1]
。这不会使您的答案的其余部分无效,但没有必要让拆分方法看起来更糟。@jpp的可读性不是很好,但根据您的字符串,拆分次数可能会更少。我尝试了,但我只能接受一个答案。当我单击“其他”时,前一个答案会变成灰色。。。
df['str'] = df['str'].str.split('_').str[-1].str[0]

print(df)

   id str
0   1   d
1   2   d
2   3   e
3   4   b
df['str'] = df['str'].str.split('(').str[0].str.split('_').str[-1]    
df

   id str
0   1   d
1   2   d
2   3   e
3   4   b
df['str'] = df['str'].str.extract(r'_([^_]+)\(', expand=False)
df

   id str
0   1   d
1   2   d
2   3   e
3   4   b
import re

p = re.compile(r'(?<=_)[^_]+(?=\()')
df['str'] = [p.search(x)[0] for x in df['str'].tolist()] 
df

   id str
0   1   d
1   2   d
2   3   e
3   4   b
df['str'] = [
    x.split('(', 1)[0].split('_')[1] for x in df['str'].tolist()
]
df

   id str
0   1   d
1   2   d
2   3   e
3   4   b
df_test = pd.concat([df] * 10000, ignore_index=True)
%timeit df_test['str'].str.extract(r'_([^_]+)\(', expand=False)
%timeit df_test['str'].str.split('(').str[0].str.split('_').str[-1] 
%timeit [p.search(x)[0] for x in df_test['str'].tolist()] 
%timeit [x.split('(', 1)[0].split('_')[1] for x in df_test['str'].tolist()]

70.4 ms ± 623 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
99.6 ms ± 730 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
31 ms ± 877 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
30 ms ± 431 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)  # fastest but not by much