[英]Find a string in a dataframe and write row to a new dataframe (Pandas, Python 3.4)
我有一個字符串列表,稱為“參與者”,例如:
'3'
'5'
'6'
...
和一個數據框,稱為“數字”:
Index name value
0 '0' 313
1 '1' 2343
2 '2' 15
3 '3' 62
4 '4' 97
5 '5' 157
6 '6' 144
...
我想創建一個新的數據框,其中僅包含“參與者”名稱的所有行。 結果應如下所示:
Index name value
0 '3' 62
1 '5' 157
2 '6' 144
...
用提取
newdf = df[df['name'].isin(participant_list)]
如果您希望索引再次從零開始計數,
reindexed_df = newdf.reset_index(drop=True)
我會按照rauparaha的建議去做。 另一個解決方案可能是:
s = "name in %s" % (tuple(participants),)
numbers.query(s)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.