我需要寫一個函式。
它將資料集中的任何值作為輸入,并應在所有行中查找交集。
例如:電話 = 87778885566
該表由以下欄位表示:
- 鑰匙
- ID
- 電話
- 電子郵件
測驗資料:
- 1個;12345; 89997776655;[email protected]
- 2;54321; 87778885566;兩個@gmail.com
- 3;98765; 87776664577;三@gmail.com
- 4;66678; 87778885566;四@gmail.com
- 5個;34567; 84547895566;四@gmail.com
- 6;34567; 89087545678;五@gmail.com
輸出應該是:
- 2;54321; 87778885566;兩個@gmail.com
- 4;66678; 87778885566; 四@gmail.com
- 5個;34567; 84547895566;四@gmail.com
- 6;34567; 89087545678;五@gmail.com
它應該檢查所有值,如果值在某處相交,則回傳具有相交的資料集。
uj5u.com熱心網友回復:
你可以使用recurssion:
import numpy as np
def relation(dat, values):
d = dat.apply(lambda x: x.isin(values.ravel()))
values1 = dat.iloc[np.unique(np.where(d)[0]),:]
if set(np.array(values)) == set(values1.to_numpy().ravel()):
return values1
else:
return relation(dat, values1.to_numpy().ravel())
relation(df.astype(str), np.array(['87778885566']))
1 2 3
1 54321 87778885566 [email protected]
3 66678 87778885566 [email protected]
4 34567 84547895566 [email protected]
5 34567 89087545678 [email protected]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/494405.html
標籤:Python python-3.x 熊猫 麻木的
上一篇:基于現有資料框創建新資料框
下一篇:回圈計算專案串列并附加
