Python 如何从下一列中选择一个值
假设我有一个csv文件,打开时看起来像这样: 代码: 输出:Python 如何从下一列中选择一个值,python,pandas,Python,Pandas,假设我有一个csv文件,打开时看起来像这样: 代码: 输出: year number 0 2001 1 1 2002 41 2 2003 123 3 2004 135 基本上,我在尝试这样做,当用户输入匹配年份时,它将从同一行返回数字 例: 用户输入=2002 输出=41 不知道如何进行任何帮助是感激的 您可以尝试: import pandas as pd table = pd.read_csv("numbers.csv") user_input =
year number
0 2001 1
1 2002 41
2 2003 123
3 2004 135
基本上,我在尝试这样做,当用户输入匹配年份时,它将从同一行返回数字
例:
用户输入=2002
输出=41
不知道如何进行任何帮助是感激的 您可以尝试:
import pandas as pd
table = pd.read_csv("numbers.csv")
user_input = int(input())
#print(table)
table.set_index('year', inplace=True)
print(table.loc[user_input]['number'])
df.loc[df['year']==user\u-input,'number']
@splash58当我尝试这个user\u-input=input()打印时(df.loc[df['annee']==user\u-input,'taux']),我得到一个错误:/python中的input()
返回一个字符串,如果您的数据帧列类型是数字,则必须将其转换为int
import pandas as pd
table = pd.read_csv("numbers.csv")
user_input = int(input())
#print(table)
table.set_index('year', inplace=True)
print(table.loc[user_input]['number'])