[英]Python replace entire string if it begin with certain character in dataframe
I have data that contains 'None ...' string at random places. 我有随机位置包含“无...”字符串的数据。 I am trying to replace a cell in the dataframe with empty character only when it begin with 'None ..'.
我试图只在以'None ..'开头时用空字符替换数据框中的单元格。 Here is what I tried, but I get errors like 'KeyError'.
这是我尝试过的方法,但出现诸如“ KeyError”之类的错误。
df = pd.DataFrame({'id': [1,2,3,4,5],
'sub': ['None ... ','None ... test','math None ...','probability','chemistry']})
df.loc[df['sub'].str.replace('None ...','',1), 'sub'] = '' # getting key error
output looking for: (I need to replace entire value in cell if 'None ...' is starting string. Notice, 3rd row shouldn't be replaced because 'None ...' is not starting character) 寻找的输出:(如果'None ...'是起始字符串,我需要替换单元格中的整个值。注意,不应替换第三行,因为'None ...'不是起始字符)
id sub
1
2
3 math None ...
4 probability
5 chemistry
You can use the below to identify the cells to replace and then assign them an empty value: 您可以使用以下内容标识要替换的单元格,然后为它们分配一个空值:
df.loc[df['sub'].str.startswith("None"), 'sub'] = ""
df.head()
id sub
0 1
1 2
2 3 math None ...
3 4 probability
4 5 chemistry
df['sub'] = df['sub'].str.replace('[\w\s]*?(None \.\.\.)[\s\w]*?','',1)
Out: 日期:
sub
id
1
2 test
3
4 probability
5 chemistry
Look at startswith
, then after we find the row need to be replaced we using replace
查看
startswith
,然后在发现需要替换的行后,我们使用replace
df['sub']=df['sub'].mask(df['sub'].str.startswith('None ... '),'')
df
Out[338]:
id sub
0 1
1 2
2 3 math None ...
3 4 probability
4 5 chemistry
You can simpy replace 'None ...' and by using a regular expression you can apply this replacement only for strings that start with None. 您可以简单地替换“ None ...”,并通过使用正则表达式可以仅对以None开头的字符串应用此替换。
df['sub'] = df['sub'].str.replace(r'^None \.\.\.*','',1)
the output looks like this: 输出看起来像这样:
id sub
0 1
1 2 test
2 3 math None ...
3 4 probability
4 5 chemistry
First, you are using the sub strings as index, that is why you received key error. 首先,您将子字符串用作索引,这就是为什么您收到键错误的原因。
Second you can do this by: df['sub']=df['sub'].apply(lambda x: '' if x.find('None')==0 else x) 其次,您可以通过以下方法执行此操作:df ['sub'] = df ['sub']。apply(lambda x:``如果x.find('None')== 0否则x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.