我有这个数据框
d = {'Number': [1, 2,3,4,5,6,7], 'Letters': ["a", "d","z","f","u","p","g"]}
df = pd.DataFrame(data=d)
Number Letters
0 1 a
1 2 d
2 3 z
3 4 f
4 5 u
5 6 p
6 7 g
我想基于Letters
列从Number
列中获取一个值
可以说我想得到数字为3的字母
我所做的是
letter = df.loc[df['Number'] == 3]
dfletter = pd.DataFrame(data=letter.values, columns = ['Number', 'Letter'])
dfletter = dfletter.drop(columns = 'Number')
这给了我我想要的东西
Letter
0 z
但这似乎是一个愚蠢的解决方法,所以我正在寻找更好的解决方案
答案 0 :(得分:1)
output = df.loc[df['Number'] == 3, 'Letters']
答案 1 :(得分:0)
>>> df[df.Number == 3].Letters
2 z
Name: Letters, dtype: object
或者,如果您确实需要标量值:
>>> df[df.Number == 3].Letters.values[0]
'z'