How to return a value from a table or dataframe given 2 inputs? Python

2024/10/9 4:22:37

Let's say this is my dataframe: enter image description here

and the user inputs B and 2

Then the function would return clemintine

Is there a way to do this without using a bunch of if elif statements. The actual dataframe I'm working with is much larger and it would take a long time to do with if elif statements.

Essentially I want the user to enter a row index value and a column index value and the function returns the corresponding value.

Answer

If you define you row starting value as 0 then you can achieve this by the following code

import pandas as pd# define sample data frame
df = pd.DataFrame({'A': ['Apple', 'Pear', 'Strawberry'],'B': ['Banana', 'Clementine', 'Blackberry'],'C': ['Strawberry', 'Blackberry', 'Peach']})# get user input
selected_row = int(input('Please enter the row that you want to specify: '))
selected_column = input('Please enter the column that you want to specify: ')# return value
cell_value = df[selected_column].iloc[selected_row]print(cell_value)
https://en.xdnf.cn/q/118631.html

Related Q&A

tkinter progressbar for multiprocessing

I have a program that encrypts files and I used multiprocessing to make it faster, but I am having trouble with the tkinter progress bar. I have implemented it but it completes immediately or lags in b…

How to add and subtract in python

So I am making a statcalc and everything is working except adding. When I select the option to add it just skips it and says select an option. I was wondering whats wrong with it?numberstoadd = input(…

Python: deferToThread XMLRPC Server - Twisted - Cherrypy?

This question is related to others I have asked on here, mainly regarding sorting huge sets of data in memory.Basically this is what I want / have:Twisted XMLRPC server running. This server keeps seve…

How do I make a linear gradient with Python Turtle?

Im currently trying to replicate this image: https://i.sstatic.net/fymWE.jpg Im trying to make that gradient in the background but I have zero clue how to do it and theres basically nothing on the inte…

Python - Converting an array to a list causes values to change

>>> import numpy as np >>> a=np.arange(0,2,0.2) >>> a array([ 0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8]) >>> a=a.tolist() >>> a [0.0, 0.2, …

Understand Python Function [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

how to download linkedin (save as pdf option) using python

Image what i want to download.Image is of LinkedIn profile page of my friend i want to click on that save-as-pdf option for many users.can that be downloaded using python code? for different users? o…

My tkinter entry box is printing .!entry instead of what is entered

from tkinter import * def _name_():businessname=entry_bnprint(businessname) edit_bar=Tk() name=Label(edit_bar,text="Name:").grid(row=0) entry_bn=Entry(edit_bar) entry_bn.grid(row=0,column=1) …

How to get an average from a row then make a list out of it [duplicate]

This question already has answers here:Reading a CSV file, calculating averages and printing said averages(2 answers)Closed 6 years ago.If I have a csv data that gives two row values of:years grades 20…

Beautiful soup: Extract everything between two tags when these tags have different ids

Beautiful soup: Extract everything between two tags I have seen a question through the above link where we are getting the information between two tags. Whereas I need to get the information between th…