Is there a way to query the df based on the row values and column header? Looking to pass through Disney
, World
, and Value
as variables to return 55
. I tried using the df.loc
function but received errors. The Key1
and Key2
headers are set as the df.index
.
Pandas Dataframe
Key1 | Key2 | Value |
---|---|---|
Disney | World | 55 |
Disney | Land | 97 |
如果 key1 和 key2 作为多索引在索引中,我认为你只需要这个。
df.loc[('Disney', 'World'), 'Value']
You can get values of 'Value' to with this code
pd.read_csv(PATH)
data = data.dropna()
Value = data['Value'].values # Get values of 'Value'
# Key2 = data['Key2'].values # Get values of 'Key2'
print(Value)
And output will be
[55, 97]
IIUC, you create a function, pass the parameters and get the value in return
df=df.set_index(['Key1', 'Key2'])
def findval(df, Key1, Key2):
return df.loc[(Key1, Key2), 'Value']
findval(df, 'Disney', 'Land')
RESULT:
97
OR
def findval(df, Key1, Key2):
return df[(df['Key1'] == Key1) &
(df['Key2'] == Key2)]['Value']
findval(df, 'Disney', 'Land')
1 97
Name: Value, dtype: int64
只需直接在系列上发出多索引表达式:
df.Value['Disney', 'World']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.