如何使用用户输入选择行列并返回单元格值? [英] How to use user input to select row column and return cell value?

查看:101
本文介绍了如何使用用户输入选择行列并返回单元格值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从csv文件构造的pandas数据框.

I have a pandas dataframe constructed from a csv file.

我要求用户输入两个值,以空格分隔.

I ask the user for two values, separated by a space.

然后我尝试执行pandas.loc [,]以返回该单元格值.

I then attempt to do a pandas.loc[,] to return that cell value.

如何将它们的输入与行列匹配,并仅返回该单元格的原始值?在Python中不可能?到目前为止,我的程序在确定要查找哪个csv表以及如何将输入分配给变量方面效果很好.实际传递给图表查找无效.这是一些示例代码.

How do I match their input to my row columns and return just the raw value of that cell? Impossible in Python? My program works well so far in determining which csv table to look up and how to assign the input to variables. 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

我将Years输入处理为一个int,然后将其输入到排序表中,使其值为'Over 0'或'Over 5'等.该字符串与索引中的术语匹配,例如此处的示例表.排名作为字符串输入,不做任何更改.

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. Rank is input as string and no changes are made.

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.

推荐答案

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

获取用户输入...注意对ij的分配.您有一个split,但只在拆分中获取了第一个值

Grab user input... Notice the assignment to i and j. You had a split but were only grabbing the first value in the split

i, j = input('>>>  ').split()
>>>  i C

A.loc[i, j]

6

这篇关于如何使用用户输入选择行列并返回单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆