在熊猫数据框中寻找价值

时间:2019-11-08 21:07:25

标签: python pandas dataframe

我有一个dataset。我想选择一个特定学生的价值。之后,我要应用“ if else”的条件,即CSC103标记是否大于70。print hi

.entry

1 个答案:

答案 0 :(得分:0)

使用提供的XLS进行了测试。

import pandas as pd

df_xlsx = pd.read_excel('FA15-BSE.xls', sheet_name = 'Final')

# note the space after the Registration #
student_reg = 'FA15-BSE-001 '

# If you want to use like this:
# student_reg = 'FA15-BSE-001'

# You need to ignore leading or trailing spaces with .str.strip()
# student_data = df_xlsx[df_xlsx['Registration #'].str.strip() == student_reg]

student_data = df_xlsx[df_xlsx['Registration #'] == student_reg]

# student_data['CSC103 (4)'] returns a <class 'pandas.core.series.Series'>
# Do print(type(student_data['CSC103 (4)'])) and see for yourself
# So you can retrieve the item with [0] like in a list
# If need first matched value you can also use `iter` with `next`: 
# if next(iter(student_data['CSC103 (4)']), 0):
# advantage is if no value is matched is returned default value (0 in this case):
if student_data['CSC103 (4)'][0] >= 70:]
    print('Hi')
else:
    print('Need to study more')

哪个产量

hi