例如,如果我有多個資料幀,如 df1、df2 和 df3。我在每個資料框中都有一列“phone_no”。如何在每個資料框中搜索 phone_no 并回傳該資料框所在的行?
例如
df_all = [df1, df2, df3]
for i in df_all:
print(i.loc[i['phone_no'] == 9999999999])
上面的代碼回傳空輸出。輸出必須是 phone_no 包含該特定電話號碼的行。如何解決這個問題?
uj5u.com熱心網友回復:
通過比較phone_no字串來檢查這是否有效:
df_all = [df1, df2, df3]
for i in df_all:
print(i.loc[i['phone_no'].astype(str) == '9999999999'])
也許你不需要轉換phone_no,就str好像它已經是這樣了。你必須檢查:
>>> print(df1['phone_no'].dtype)
object
# OR
>>> print(df1['phone_no'].dtype)
int64
更新
df_all = [df1, df2, df3]
df_filtered = []
for i in df_all:
df_filtered.append(i.loc[i['phone_no'].astype(str) == '9999999999'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/446268.html
標籤:Python python-3.x 熊猫 数据框 for循环
