Pandas:删除某个子串不存在的行[重复]

5sxhfpxr  于 2023-10-14  发布在  其他
关注(0)|答案(2)|浏览(92)

此问题已在此处有答案

Filter pandas DataFrame by substring criteria(17回答)
4天前关闭。
所以我现在有这个我对这个东西很陌生

import pandas as pd

file_name = 'Should_work2222.csv'

df = pd.read_csv(file_name)[['Assignees_y', 'Serials']]

result = pd.DataFrame(df)

print(result)

这段代码为我工作,我得到了我想要的,但我试图只接收某些单词,如Fastenal与序列号,我知道我可以使用Excel只得到我想要的信息,但我想尝试使用Python

Assignees_y     Serials
0      Fastenal-OB(any)-locker 110  376txk1233
1             Fastenal - locker 70  376TXK1235
2              Fastenal-locker 105  376txk1244
3           Fastenal - OB Dock 111  376txk6431
4                              bin  376txr4241
5                          Missing  376txr5214
6                             lost  376txk5352
7                             lost  376txk1324
8                             lost  376TXR6446
9              available in master  376TXK5335
10                      IB Receive  376txk1424
11                      IB Receive  376txk1601
12                      IB Receive  376TYR5366
13                      IB Receive  376TXR5535
14     Fastenal - IB Rcv locker 91  376txk6434
15      Fastenal - IB Rcvlocker 93  376txr5553
16     Fastenal - IB Rcv locker 94  376TXR5333
17  Fastenal - IB Stw - locker 102  376TXK3532
18  Fastenal - IB Stw - locker 103  376txk5353
19                          IB Rcv  376txk5322
20                            lost  376txk2625
21                              OB  376txk2434
22        Fastenal-pick- locker 82  376txk6454
23         Fastenal-pack-locker 84  376txk5343
24         Fastenal-pack-locker 90  376TXK1244

我想把这个处理掉只买Fastenal和他们的系列

h9a6wy2h

h9a6wy2h1#

使用str.contain方法:

df.loc[df.Assignees_y.str.contains('Fastenal')]

                       Assignees_y     Serials
0      Fastenal-OB(any)-locker 110  376txk1233
1             Fastenal - locker 70  376TXK1235
2              Fastenal-locker 105  376txk1244
3           Fastenal - OB Dock 111  376txk6431
14     Fastenal - IB Rcv locker 91  376txk6434
15      Fastenal - IB Rcvlocker 93  376txr5553
16     Fastenal - IB Rcv locker 94  376TXR5333
17  Fastenal - IB Stw - locker 102  376TXK3532
18  Fastenal - IB Stw - locker 103  376txk5353
22        Fastenal-pick- locker 82  376txk6454
23         Fastenal-pack-locker 84  376txk5343
24         Fastenal-pack-locker 90  376TXK1244
l2osamch

l2osamch2#

删除不包含文本“Fastenal”的所有行:

from io import StringIO
import pandas as pd

stream = StringIO("""
Assignees_y,Serials
Fastenal-OB(any)-locker 110,376txk1233
Fastenal - locker 70,376TXK1235
Fastenal-locker 105,376txk1244
Fastenal - OB Dock 111,376txk6431
bin,376txr4241
Missing,376txr5214
lost,376txk5352
lost,376txk1324
lost,376TXR6446
available in master,376TXK5335
IB Receive,376txk1424
IB Receive,376txk1601
IB Receive,376TYR5366
IB Receive,376TXR5535
Fastenal - IB Rcv locker 91,376txk6434
Fastenal - IB Rcvlocker 93,376txr5553
Fastenal - IB Rcv locker 94,376TXR5333
Fastenal - IB Stw - locker 102,376TXK3532
Fastenal - IB Stw - locker 103,376txk5353
IB Rcv,376txk5322
lost,376txk2625
OB,376txk2434
Fastenal-pick- locker 82,376txk6454
Fastenal-pack-locker 84,376txk5343
Fastenal-pack-locker 90,376TXK1244
""")
df = pd.read_csv(stream)

mask = df["Assignees_y"].str.contains("Fastenal")
df = df.loc[mask]

print(df)

它打印:

Assignees_y     Serials
0      Fastenal-OB(any)-locker 110  376txk1233
1             Fastenal - locker 70  376TXK1235
2              Fastenal-locker 105  376txk1244
3           Fastenal - OB Dock 111  376txk6431
14     Fastenal - IB Rcv locker 91  376txk6434
15      Fastenal - IB Rcvlocker 93  376txr5553
16     Fastenal - IB Rcv locker 94  376TXR5333
17  Fastenal - IB Stw - locker 102  376TXK3532
18  Fastenal - IB Stw - locker 103  376txk5353
22        Fastenal-pick- locker 82  376txk6454
23         Fastenal-pack-locker 84  376txk5343
24         Fastenal-pack-locker 90  376TXK1244

相关问题