簡體   English   中英

在 pandas dataframe 中查找值

[英]finding value in pandas dataframe

我有一個數據集 我想為特定學生的 select 值。 之后我想應用“if else”的條件意味着如果 CSC103 標記大於 70。打印 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM