[英]How to made a slice in a pandas dataframe?
I want do a slice a dataframe using a string "PP" that is in my column and get just the numbers that is afeter string:我想使用列中的字符串“PP”对 dataframe 进行切片,并仅获取字符串之后的数字:
Dataframe: Dataframe:
data = {'Serie':['28PP3097', '23228PP3097', '1822343218PP3097', '43642183097'],
'FooBar':["foo", "bar", "foo", "bar"]}
df = pd.DataFrame(data)
Expected Result:预期结果:
I try:我尝试:
df["Serie"] = np.where(df["Serie"].str.contains("PP"), df["Serie"][df["Serie"].str.find('PP')+1:],df["Serie"])
In this df, but it give me a erro ´cannot do slice indexing on RangeIndex with these indexers´在这个 df 中,但它给了我一个错误“无法使用这些索引器对 RangeIndex 进行切片索引”
You can do that by splitting and getting the last item after "PP":您可以通过拆分并获取“PP”之后的最后一项来做到这一点:
data = {'Serie':['28PP3097', '23228PP3097', '1822343218PP3097', '43642183097'],
'FooBar':["foo", "bar", "foo", "bar"]}
df = pd.DataFrame(data)
df['Serie']=[i.split('PP')[-1] for i in df['Serie']]
Result结果
Serie FooBar
0 3097 foo
1 3097 bar
2 3097 foo
3 43642183097 bar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.