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

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

此问题已在此处有答案

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

  1. import pandas as pd
  2. file_name = 'Should_work2222.csv'
  3. df = pd.read_csv(file_name)[['Assignees_y', 'Serials']]
  4. result = pd.DataFrame(df)
  5. print(result)

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

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

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

h9a6wy2h

h9a6wy2h1#

使用str.contain方法:

  1. df.loc[df.Assignees_y.str.contains('Fastenal')]
  2. Assignees_y Serials
  3. 0 Fastenal-OB(any)-locker 110 376txk1233
  4. 1 Fastenal - locker 70 376TXK1235
  5. 2 Fastenal-locker 105 376txk1244
  6. 3 Fastenal - OB Dock 111 376txk6431
  7. 14 Fastenal - IB Rcv locker 91 376txk6434
  8. 15 Fastenal - IB Rcvlocker 93 376txr5553
  9. 16 Fastenal - IB Rcv locker 94 376TXR5333
  10. 17 Fastenal - IB Stw - locker 102 376TXK3532
  11. 18 Fastenal - IB Stw - locker 103 376txk5353
  12. 22 Fastenal-pick- locker 82 376txk6454
  13. 23 Fastenal-pack-locker 84 376txk5343
  14. 24 Fastenal-pack-locker 90 376TXK1244
l2osamch

l2osamch2#

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

  1. from io import StringIO
  2. import pandas as pd
  3. stream = StringIO("""
  4. Assignees_y,Serials
  5. Fastenal-OB(any)-locker 110,376txk1233
  6. Fastenal - locker 70,376TXK1235
  7. Fastenal-locker 105,376txk1244
  8. Fastenal - OB Dock 111,376txk6431
  9. bin,376txr4241
  10. Missing,376txr5214
  11. lost,376txk5352
  12. lost,376txk1324
  13. lost,376TXR6446
  14. available in master,376TXK5335
  15. IB Receive,376txk1424
  16. IB Receive,376txk1601
  17. IB Receive,376TYR5366
  18. IB Receive,376TXR5535
  19. Fastenal - IB Rcv locker 91,376txk6434
  20. Fastenal - IB Rcvlocker 93,376txr5553
  21. Fastenal - IB Rcv locker 94,376TXR5333
  22. Fastenal - IB Stw - locker 102,376TXK3532
  23. Fastenal - IB Stw - locker 103,376txk5353
  24. IB Rcv,376txk5322
  25. lost,376txk2625
  26. OB,376txk2434
  27. Fastenal-pick- locker 82,376txk6454
  28. Fastenal-pack-locker 84,376txk5343
  29. Fastenal-pack-locker 90,376TXK1244
  30. """)
  31. df = pd.read_csv(stream)
  32. mask = df["Assignees_y"].str.contains("Fastenal")
  33. df = df.loc[mask]
  34. print(df)

它打印:

  1. Assignees_y Serials
  2. 0 Fastenal-OB(any)-locker 110 376txk1233
  3. 1 Fastenal - locker 70 376TXK1235
  4. 2 Fastenal-locker 105 376txk1244
  5. 3 Fastenal - OB Dock 111 376txk6431
  6. 14 Fastenal - IB Rcv locker 91 376txk6434
  7. 15 Fastenal - IB Rcvlocker 93 376txr5553
  8. 16 Fastenal - IB Rcv locker 94 376TXR5333
  9. 17 Fastenal - IB Stw - locker 102 376TXK3532
  10. 18 Fastenal - IB Stw - locker 103 376txk5353
  11. 22 Fastenal-pick- locker 82 376txk6454
  12. 23 Fastenal-pack-locker 84 376txk5343
  13. 24 Fastenal-pack-locker 90 376TXK1244
展开查看全部

相关问题