[英]How to remove rows with more than one value in a cell in Pandas
我有一個如下所示的數據框:
receiver_id sender_id
a,b,d c
a,d b
b a
a b
我想刪除包含多個receiver_id
的行。 所以最終的數據幀應該只有第 3 行和第 4 行。我應該如何 go 這樣做?
所需的 output:
receiver_id sender_id
b a
a b
您可以 boolean 通過查找逗號對數據幀進行切片,假設多個值是單個字符串而不是列表。
df = df[~df.receiver_id.str.contains(',')].reset_index(drop=True)
這應該有效:
df.loc[df['receiver_id'].str.count(',').eq(0)]
嘗試這個:
df = df[df['receiver_id'].str.split(',').str.len()<2]
如果這已經是列中的列表,您可以將其簡化為:
df = df[df['receiver_id'].str.len()<2]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.