鑒于以下熊貓資料框df-
| 報告組 | 物體/分組 | 物體 ID | 調整后的價值(今天,無除數,美元) | 調整后的 TWR(當前季度,無 Div,美元) | 調整后的 TWR(年初至今,無 Div,美元) | 年化調整 TWR(自成立以來,無 Div,美元) | 調整后的價值(無 Div,美元) |
|---|---|---|---|---|---|---|---|
| 兵營家庭 | 威廉和魯珀特信托 | 9957007 | -1.44 | -1.44 | |||
| 兵營家庭 | 現金 | - | -1.44 | -1.44 | |||
| 兵營家庭 | Gratia Holdings No. 2 LLC | 8413655 | 55491732.66 | -0.971018847 | -0.971018847 | 11.52490309 | 55491732.66 |
| 兵營家庭 | 投資級固定收益 | - | 18469768.6 | 18469768.6 | |||
| 兵營家庭 | 高收益固定收益 | - | 3668982.44 | -0.205356545 | -0.205356545 | 4.441190127 | 3668982.44 |
我試圖只保留Entity/Grouping列值 ==Cash 并且以下列值中的任何一個的行NaN-
Adjusted TWR (Current Quarter, No Div, USD)Adjusted TWR (YTD, No Div, USD)Annualized Adjusted TWR (Since Inception, No Div, USD)
當前代碼:以下函式接受df并嘗試執行上述操作并回傳,準備在另一個函式中使用 -
def twr_exceptions_logic():
df = databases_creation()
df = df.loc[(df['Entity/Grouping']!= 'Cash')]
df = df.loc[(df['Adjusted TWR (Current Quarter, No Div, USD)',
'Adjusted TWR (YTD, No Div, USD)',
'Annualized Adjusted TWR (Since Inception, No Div, USD)'].isnull())]
return df
問題/錯誤:目前,我得到一個非描述性的TypeError,它參考了我參考的列:KeyError: ('Adjusted TWR (Current Quarter, No Div, USD)', 'Adjusted TWR (YTD, No Div, USD)', 'Annualized Adjusted TWR (Since Inception, No Div, USD)')
幫助:我是否遺漏了一些明顯的東西?我有明顯的語法錯誤嗎?任何提示/提示都會受到熱烈歡迎。
uj5u.com熱心網友回復:
- 你還需要一個方括號。
- 您可以使用
any方法來制作適當的條件。
代碼:
import pandas as pd
def databases_creation():
import numpy as np
return pd.DataFrame({'Reporting Group': {0: 'Barrack Family', 1: 'Barrack Family', 2: 'Barrack Family', 3: 'Barrack Family', 4: 'Barrack Family'}, 'Entity/Grouping': {0: 'William and Rupert Trust', 1: 'Cash', 2: 'Gratia Holdings No. 2 LLC', 3: 'Investment Grade Fixed Income', 4: 'High Yield Fixed Income'}, 'Entity ID': {0: '9957007', 1: '-', 2: '8413655', 3: '-', 4: '-'}, 'Adjusted Value (Today, No Div, USD)': {0: -1.44, 1: -1.44, 2: 55491732.66, 3: 18469768.6, 4: 3668982.44}, 'Adjusted TWR (Current Quarter, No Div, USD)': {0: np.NaN, 1: np.NaN, 2: -0.971018847, 3: np.NaN, 4: -0.205356545}, 'Adjusted TWR (YTD, No Div, USD)': {0: np.NaN, 1: np.NaN, 2: -0.971018847, 3: np.NaN, 4: -0.205356545}, 'Annualized Adjusted TWR (Since Inception, No Div, USD)': {0: np.NaN, 1: np.NaN, 2: 11.52490309, 3: np.NaN, 4: 4.441190127}, 'Adjusted Value (No Div, USD)': {0: -1.44, 1: -1.44, 2: 55491732.66, 3: 18469768.6, 4: 3668982.44}})
def twr_exceptions_logic():
df = databases_creation()
# df = df.loc[(df['Entity/Grouping']!= 'Cash')]
# df = df.loc[(df['Adjusted TWR (Current Quarter, No Div, USD)',
# 'Adjusted TWR (YTD, No Div, USD)',
# 'Annualized Adjusted TWR (Since Inception, No Div, USD)'].isnull())]
mask = (df['Entity/Grouping']!= 'Cash') & (df[['Adjusted TWR (Current Quarter, No Div, USD)',
'Adjusted TWR (YTD, No Div, USD)',
'Annualized Adjusted TWR (Since Inception, No Div, USD)']].isnull().any(axis=1))
df = df[mask]
return df
df = twr_exceptions_logic()
print(df)
輸出:
| 報告組 | 物體/分組 | 物體 ID | 調整后的價值(今天,無除數,美元) | 調整后的 TWR(當前季度,無 Div,美元) | 調整后的 TWR(年初至今,無 Div,美元) | 年化調整 TWR(自成立以來,無 Div,美元) | 調整后的價值(無 Div,美元) |
|---|---|---|---|---|---|---|---|
| 兵營家庭 | 威廉和魯珀特信托 | 9957007 | -1.44 | 楠 | 楠 | 楠 | -1.44 |
| 兵營家庭 | 投資級固定收益 | - | 1.84698e 07 | 楠 | 楠 | 楠 | 1.84698e 07 |
uj5u.com熱心網友回復:
選擇串列時需要一個方括號:
df[['Adjusted TWR (Current Quarter, No Div, USD)',
'Adjusted TWR (YTD, No Div, USD)',
'Annualized Adjusted TWR (Since Inception, No Div, USD)']].isnull()
這解決了 KeyError。但是,使用它會給你一個新的錯誤,因為這會回傳一個二維矩陣作為引數傳遞給 df.loc,這是一個無效的引數型別。相反,您可以一一過濾它們。
def twr_exceptions_logic():
df = databases_creation()
df = df.loc[(df['Entity/Grouping']!= 'Cash')]
df = df.loc[(df['Adjusted TWR (Current Quarter, No Div, USD)'].isnull())]
df = df.loc[(df['Adjusted TWR (YTD, No Div, USD)'].isnull())]
df = df.loc[(df['Annualized Adjusted TWR (Since Inception, No Div, USD)'].isnull())]
return df
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/443070.html
