i have the following example DataFrame
df2 = pd.DataFrame(np.array([[5,0,-3],['nyc','sf','chi']]), columns=['col1','col2','col3'], index=['value','location'])
for this example, i want to retrieve the number in the 'value' row where the 'location' = 'nyc', so in this case, i want to retrieve the number 5. i am trying to use the .loc with a boolean but can't quite get it right
You may need two .loc
df2.loc['value',df2.loc['location']=='nyc']
Out[269]:
col1 5
Name: value, dtype: object
If only need value output
df2.loc['value',df2.loc['location']=='nyc'].iloc[0]
Out[270]: '5'
This will get you the value in the value
row, but I am indexing it using iloc
instead of loc
:
df2.iloc[0, np.where(df2.loc['location', ] == 'nyc')[0]].values
OUTPUT:
array(['5'], dtype=object)
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.