我现在用的是最原始的方法,代码如下:
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()