[英]How to use user input to select row column and return cell value?
I have a pandas dataframe constructed from a csv file. 我有一个从csv文件构造的pandas数据帧。
I ask the user for two values, separated by a space. 我向用户询问两个值,用空格分隔。
I then attempt to do a pandas.loc[,] to return that cell value. 然后我尝试做一个pandas.loc [,]来返回该单元格值。
How do I match their input to my row columns and return just the raw value of that cell? 如何将其输入与我的行列匹配并仅返回该单元格的原始值? Impossible in Python?
Python不可能? My program works well so far in determining which csv table to look up and how to assign the input to variables.
到目前为止,我的程序在确定要查找哪个csv表以及如何将输入分配给变量方面效果很好。 It's the actual passing to the chart lookup that doesn't work.
这是实际传递到图表查找不起作用。 Here is some sample code.
这是一些示例代码。
import pandas as pd
A_read = pd.read_csv('My.csv')
A1 = pd.DataFrame(A_read)
A = A1.set_index("Years")
separator = ' '
user_input = raw_input("Enter two values: ").split(separator)
rank = user_input[0]
Years = int(user_input[1])
pay = A.loc[ , ] #problem area
I process the Years input into an int and then input it to a sorting table giving it the value of 'Over 0' or 'Over 5' etc. This string matches the terms in my index like in the example table here. 我将Years输入处理为一个int,然后将其输入到一个排序表,给它的值为'Over 0'或'Over 5'等。这个字符串匹配我的索引中的术语,就像这里的示例表中一样。 Rank is input as string and no changes are made.
Rank作为字符串输入,不进行任何更改。
Years E-9 E-8
Over 0 40 80
Over 2 30 20
I only wrote a lot to be clear! 我只写了很多东西要清楚! Just looking for any solutions that takes inputs and matches them to rows/columns to return a cell.
只是寻找任何需要输入的解决方案,并将它们与行/列匹配以返回单元格。
Let A
be 让
A
成为
A = pd.DataFrame(
np.random.randint(10, size=(10, 10)),
list('abcdefghij'), list('ABCDEFGHIJ'))
A B C D E F G H I J
a 1 8 0 3 5 7 7 2 7 1
b 7 7 6 6 1 1 3 4 3 9
c 8 4 9 4 6 7 2 8 1 5
d 1 0 2 4 0 5 1 7 9 6
e 9 0 3 2 3 6 6 9 5 0
f 5 6 2 0 7 9 2 0 8 0
g 7 7 0 5 2 6 1 9 0 1
h 5 8 0 7 4 0 7 3 1 7
i 9 9 6 2 7 7 3 5 0 9
j 3 1 8 0 1 8 9 7 5 9
Grab user input... Notice the assignment to i
and j
. 抓取用户输入...注意
i
和j
的分配。 You had a split
but were only grabbing the first value in the split 你有一个
split
但只是抓住了split
中的第一个值
i, j = input('>>> ').split()
>>> i C
A.loc[i, j]
6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.