我有一个dataset 。我想选择特定学生的值。之后我想应用“if else”条件,意思是如果 CSC103 分数大于 70。 print hi
df_xlsx =pd.read_excel('/content/FA15-BSE.xlsm' ,sheet_name = 'Final')
df = df_xlsx[df_xlsx['Registration#'] == FA15-BSE-081]
df
if df.CSC103 >= 70:
print('hi')
最佳答案
使用提供的 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
关于python - 在 pandas 数据框中寻找值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58773813/