目標
我正在嘗試創建一個主資料框。在這個主 DataFrame 中,利用“name”列為“name”中的每個值提供一個 DataFrame,該 DataFrame 是主 DataFrame 的副本。隨著“名稱”列的增長,DataFrame 的數量也會增長。這是通過使用 DataFrame 的字典來完成的,這樣我就可以輕松地參考每個人的 DataFrame。
之后,我將過濾器應用于每個人各自的 DataFrame。
我面臨的問題
- 代碼給出了一個“名稱”錯誤,參考名為“名稱”的列。此問題出現在最后一行代碼中。
代碼
import pandas as pd
import copy
df = {'name': ['bob', 'alex', 'ryan', 'andrew', 'goliath'], 'right_arm_length': [1, 2, 3, 8, 7], 'left_arm_length': [3, 4, 2, 5, 8]}
df = pd.DataFrame(data=df) #Create main dataframe
dictionary_df={} #Create empty dictionary to store dataframes in
for index, value in df['name'].items():
dictionary_df[value '_df'] = copy.deepcopy(df) #This will create a dataframe for each person
for idx in range(len(df)): #Filtration process begins here
if idx != index: #Prevent from comparing person to themselves
dictionary_df[value '_df'] = dictionary_df[value '_df'][dictionary_df[value '_df']['name'] == value]['right_arm_length'] < dictionary_df[value '_df']['left_arm_length'][idx] ####**Issue is here**
注意:最后一行用于dictionary_df[value '_df']在應用 DataFrame 的所有者right_arm_length小于所有其他 person 的條件后進行更新left_arm_length。所以對于bob_df,它會與bob所有right_arm_length其他left_arm_length人相比bob_df。
uj5u.com熱心網友回復:
您可以創建一個布爾掩碼并迭代過濾df:
out = {}
for name in df['name']:
msk = df['name']==name
out[f'{name}_df'] = df[(df.loc[msk, 'right_arm_length'].iat[0] < df['left_arm_length']) | msk]
輸出:
{'bob_df': name right_arm_length left_arm_length
0 bob 1 3
1 alex 2 4
2 ryan 3 2
3 andrew 8 5
4 goliath 7 8,
'alex_df': name right_arm_length left_arm_length
0 bob 1 3
1 alex 2 4
3 andrew 8 5
4 goliath 7 8,
'ryan_df': name right_arm_length left_arm_length
1 alex 2 4
2 ryan 3 2
3 andrew 8 5
4 goliath 7 8,
'andrew_df': name right_arm_length left_arm_length
3 andrew 8 5,
'goliath_df': name right_arm_length left_arm_length
4 goliath 7 8}
如果你有 Python >=3.8,你可以使用 walrus 運算子并將上面的回圈轉換為 dict 理解:
out = {f'{name}_df': df[(df.loc[(msk := df['name']==name), 'right_arm_length'].iat[0] < df['left_arm_length']) | msk] for name in df['name']}
uj5u.com熱心網友回復:
你能檢查一下這是不是你想要的嗎?
dictionary_df = {}
for index, value in df['name'].items():
dictionary_df[value '_df'] = copy.deepcopy(df)
right_arm_length = dictionary_df[value '_df'][dictionary_df[value '_df']['name']==value]['right_arm_length'].iloc[0]
dictionary_df[value '_df'].loc[dictionary_df[value '_df']['name']!=value] = dictionary_df[value '_df'][dictionary_df[value '_df']['left_arm_length'] > right_arm_length]
dictionary_df[value '_df'] = dictionary_df[value '_df'].dropna()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447806.html
標籤:Python python-3.x 熊猫 数据框 字典
