如何在Dataframe中,根据某列的值取另外一列的某个值的标量?

我现在用的是最原始的方法,代码如下:

import pandas as pd
import numpy as np

df = pd.DataFrame()
df[['A', 'B']] = pd.DataFrame(np.arange(10).reshape((5, 2)))
df = df.loc[df['A'] == 4, 'B'].values[0]
print(df)

结果

5

请问,这行能再简化么?

df = df.loc[df['A'] == 4, 'B'].values[0]

大牛回答的:

方法1:如果一直能匹配使用Series.to_numpy()代替Series.values

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.to_numpy.html

df = df.loc[df['A'] == 4, 'B'].to_numpy()[0]

方法2:如果可能不匹配,使用next和iter1,因为使用.loc可能出错

a = next(iter(df.loc[df['A'] == 4, 'B']), 'no match')
print (a)
5

a = next(iter(df.loc[df['A'] == 1000, 'B']), 'no match')
print (a)
no match

方法3:

df1=df.loc[df['A'] == 4, 'B'].item()

这种不知道算不算简单了

import pandas as pd
import numpy as np
 
df = pd.DataFrame()
df[['A', 'B']] = pd.DataFrame(np.arange(10).reshape((5, 2)))

# 变成字典,以A为keys
df.set_index('A').T.to_dict('list')[4][0]

 

大牛回答的:

方法1:如果一直能匹配使用Series.to_numpy()代替Series.values

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.to_numpy.html

import pandas as pd
import numpy as np
df = pd.DataFrame()
df[['A', 'B']] = pd.DataFrame(np.arange(10).reshape((5, 2)))
df = df.loc[df['A'] == 4, 'B'].to_numpy()[0]
print(df)

方法2:如果可能不匹配,使用next和iter1,因为使用.loc可能出错

a = next(iter(df.loc[df['A'] == 4, 'B']), 'no match')
print (a)
5

a = next(iter(df.loc[df['A'] == 1000, 'B']), 'no match')
print (a)
no match

方法3:

df1=df.loc[df['A'] == 4, 'B'].item()

 

new_df=np.array(df)
res=np.where(new_df==4)[0]
new_df[res,1]