简体   繁体   English

如何使用用户输入来选择行列和返回单元格值?

[英]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 . 抓取用户输入...注意ij的分配。 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.

相关问题 如何返回某个值的单元格的列和行索引 - How to return column&row index of cell of certain value Excel:如何在包含字符串的列中查找单元格并在另一列行中返回单元格的值? - Excel: How to find cell in a column that contains a string and return value of a cell in another column of row? 如何在包含字符串的列中查找单元格并在另一行中返回单元格的值? - How to find cell in a column that contains a string and return value of a cell in another of row? 在Pandas数据帧中查找任何单元格值= = x,并返回单元格值,列标题,行和相邻单元格值 - Find any cell values >= x in Pandas dataframe and return cell value, column header, row and neighbouring cell value xlwings:如何引用单元格并将该单元格中的值用作行范围值 - xlwings: how to reference a cell and use the value in that cell as a row range value 如何使用python shift函数通过使用特定列的前一行值来增加单元格的值? - How to use python shift function to increase the values of a cell by using the previous row value of that particular column? 如何使用行中单元格的值来选择在 pandas dataframe 中查找列名? - How can I use the value of a cell in a row to chose find a column name in a pandas dataframe? 尝试通过匹配行值来 select 列,但返回该列中另一行的值 - Trying to select a column by matching a row value but return the value of another row in that column 字典和输入:如何在值中使用用户输入 - Dictionary and Input: How to use the user input in the value 如何使用用户输入在 pandas 中创建列 - How to use user input to create a column in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM