简体   繁体   English

删除 pandas df 中的列表字符串开头

[英]Remove list string startswith in pandas df

i have df rows contains lists and wants to remove the particular string combined with others.我有 df 行包含列表,并希望删除与其他字符串组合的特定字符串。

df['res']: df['res']:

AL1 A 15, CY1 A 16, CY1 A 20, GL1 A 17, GL1 A 62,HOH A 604, HOH A 605, L21 A 18, MG A 550, PR1 A 36, TH1 A 19, TH1 A 37, TY1 A 34, VA1 A 14, HOH A 603, VA1 A 35

Desired output: [ removed HOH with other number]所需的 output:[用其他数字删除 HOH]

AL1 A 15, CY1 A 16, CY1 A 20, GL1 A 17, GL1 A 62, L21 A 18, MG A 550, PR1 A 36, TH1 A 19, TH1 A 37, TY1 A 34, VA1 A 14, VA1 A 35

I tried this:我试过这个:

data['res'].str.split().apply(lambda x: [k for k in x if k.startswith('HOH')])

The problem is that if you use .split() without anything else every substring will also get split.问题是,如果你使用.split()而不使用其他任何东西,每个 substring 也会被拆分。

So this ... ,HOH A 604... will split into ['...', ',','HOH', 'A', '604', '...'] .所以这... ,HOH A 604...将分成['...', ',','HOH', 'A', '604', '...']

As far as I understood you want to remove every HOH with the following numbers right?据我了解,您想删除带有以下数字的每个HOH对吗?

Doing it the .split() way will result in removing HOH only and keeping A & 604 .这样做.split()方式将导致仅删除HOH并保留A & 604

If you use .split(',') with the comma as parameter then we will get everything between commas seperated.如果您使用.split(',')和逗号作为参数,那么我们将得到逗号分隔的所有内容。

The problem I see with startswith is that sometimes your strings have an additional space after the comma and sometimes they don´t (eg , HOH A 604 &, HOH A 605 )我看到的问题startswith是有时你的字符串在逗号后有一个额外的空格,有时它们没有(例如, HOH A 604 &, HOH A 605

Therefore I would suggest to use not in instead.因此,我建议使用not in代替。 BUT: aware that this removes all sub strings that contain HOH even if they are at the end.但是:请注意,这会删除所有包含HOH的子字符串,即使它们位于末尾。

try this:尝试这个:

df['res'].str.split(',').apply(lambda x: [k for k in x if 'HOH' not in k])

The cell value is now a list of strings if you need to have a string again try this:如果您需要再次使用字符串,则单元格值现在是字符串列表,请尝试以下操作:

df['res'].str.split(',').apply(lambda x: ','.join([k for k in x if 'HOH' not in k]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM