我正在尝试删除在保留第一个值时用'['标识的残留数据。
import pandas as pd
df=pd.DataFrame({'foo':['a','b[b7','c']})
print(df)
成为:
0 a
1 b[b7
2 c
想要
0 a
1 b
2 c
有什么建议吗?
答案 0 :(得分:1)
df.foo=df.foo.str[0]
df
Out[212]:
foo
0 a
1 b
2 c
答案 1 :(得分:1)
我认为您正在寻找str.split
+ str[0]
-
df
foo
0 test
1 foo[b7
2 ba[r
df.foo.str.split('[').str[0]
0 test
1 foo
2 ba
Name: foo, dtype: object
答案 2 :(得分:0)
import pandas as pd
df = pd.DataFrame({'foo':[x.split('[')[0] for x in ['a','b[b7','c']]})
print(df)
答案 3 :(得分:0)
import pandas as pd
df=pd.DataFrame({'foo':['a','b[b7','c']} )
df["foo"] = df["foo"].str.replace("(\[.*)","")
1st Capturing Group (\[.*)
\[ matches the character [ literally (case sensitive)
.* matches any character (except for line terminators)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
这意味着它会寻找[。如果找到一个,它将删除[及其后面的所有字符。