pandas 会删除字符后一列中的所有字符串 [英] Pandas remove all of a string in a column after a character
问题描述
所以我有一个超过500行的数据集,其中一列的值是这样的:
So I have a data set with over 500 rows where one of the columns has values like this:
df:
column1
0 a{'...'}
1 b{'...'}
2 c{'...'}
3 d{'...'}
我想删除{}
内的所有内容.
I want to remove everything within and including the {}
.
我一直在看这个问题,熊猫在数据框内的指定字符后删除字符串的一部分,并在那里尝试解决方案,但我不断收到错误消息(而且我知道StringIO
现在是io.StringIO
).
I have been looking at this question, Pandas delete parts of string after specified character inside a dataframe and tried the solutions there but I keep getting errors(And I am aware that StringIO
is now io.StringIO
).
我尝试过
df.column1 = df.column1.str.split('{')[0]
但收到错误消息:KeyError: 0
并不太了解这是什么意思
but get the error message: KeyError: 0
and don't really understand what that means
我也尝试过:
df.column1 = df.column1.str.split(pat='{')
但这似乎只是删除了"{",所以我只剩下了
But this only seems deletes the '{' so I'm left with
column1
0 a'...'}
1 b'...'}
2 c'...'}
3 d'...'}
我也不确定它是否重要,但是列是object
类型.
谁能告诉我我在做什么错以及如何解决该问题???
Also I'm not sure if it's important but the column is an object
type.
Can anyone tell me what I'm doing wrong and how to fix the issue???
推荐答案
您可以使用replace
df['column1'].str.replace(r"\{.*\}","")
Out[385]:
0 a
1 b
2 c
3 d
Name: column1, dtype: object
这篇关于 pandas 会删除字符后一列中的所有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!