python - 在 pandas 数据框中寻找值(value)

标签 python pandas dataframe

我有一个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/

相关文章:

python - 禁用 "\"字符 Python 2.7 的自动加倍 - re2 错误

python - 如何通过将列的值与没有迭代的字符串进行比较来获取数据帧的索引

python - 将列按名称 move 到 pandas 中的表前面

从 R 中的数据框中删除重复的列组合

javascript - Bokeh 中的自定义 javascript 回调可从数据框中选择列并更新绘图

python - 更改 pandas Dataframe 中日期时间的值

python - Pylint 不能在 OS X 上使用 Emacs GUI;从命令行工作

python - 查找相似的数据并将它们放在单独的列表中

python - 当元素属于多个类别时按类别分组

python - 如何将 pandas 数据框中的时间戳转换为 datetime.date?