答案 0 :(得分:1)
首先将新的列名称添加到列表中,例如Col1
:
math = [['Jason',100,35],
['Alice',87.89,100],
['Mary',68,0]]
df = pd.DataFrame(math, columns=['Name','Grade', 'Col1'])
print (df)
Name Grade Col1
0 Jason 100.00 35
1 Alice 87.89 100
2 Mary 68.00 0
然后按boolean indexing
和DataFrame.loc
进行过滤,以按Name
和列名进行过滤:
a = df.loc[df['Name'] == 'Alice', 'Grade']
print (a)
1 87.89
Name: Grade, dtype: float64
然后将第一个匹配值与next
一起使用iter
:
val = next(iter(a), 'no match')
print (val)
87.89
...如果不匹配,应使用什么来返回默认值:
a = df.loc[df['Name'] == 'William', 'Grade']
print (a)
Series([], Name: Grade, dtype: float64)
val = next(iter(a), 'no match')
print (val)
no match