function that takes one column value and returns another column value

2024/10/13 23:16:36

Apologies, if this is a duplicate please let me know, I'll gladly delete.

My dataset:

Index     Col 1      Col 20         1       4, 5, 6   
1         2       7, 8, 9
2         3       10, 11, 12   
3         4       13, 14, 15

I am attempting to create a function that will take a particular column 1 value as its input and output the column 1 value with its corresponding column 2 values.

For example, if I used the function for when column 1 is equal to 3, the function would return a list of 4 values: 3, 10, 11, 12

Many thanks

Answer
def f(a):return df.loc[df['Col 1'] == a, 'Col 2'].item()

But if need more general:

print (df)Col 1       Col 2
0      1     4, 5, 6
1      1     7, 8, 9
2      3  10, 11, 12
3      4  13, 14, 15def f(a):a = df.loc[df['Col 1'] == a, 'Col 2']#for no matchif a.empty:return 'no match'#for multiple matchelif len(a) > 1:return aelse:#for match one value onlyreturn a.item()print (f(1))
0    4, 5, 6
1    7, 8, 9
Name: Col 2, dtype: objectprint (f(3))
10, 11, 12print (f(6))
no match
https://en.xdnf.cn/q/118026.html

Related Q&A

Python write value from dictionary based on match in range of columns

From my df showing employees with multiple levels of managers (see prior question here), I want to map rows to a department ID, based on a manager ID that may appear across multiple columns:eid, mid…

Merge two dataframes based on a column

I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ? df1place name qty unit…

Python/Pandas - building a new column based in columns comparison

I have this dataframe:df:CNPJ Revenues 2016 Revenues 2015 Revenues 2014 0 01.637.895/0001-32 R$ 12.696.658 NaN R$ 10.848.213 1 02.916.265/0001-60 …

Present blank screen, wait for key press -- how?

lo,I am currently trying to code a simple routine for an experiment we are planning to run. The experiment starts by entering a subject number and creating a bunch of files. I got that part working. Ne…

Images appearing in all but 1 flask page

I am creating a web app in flask, python, mysql. When viewing every other page on my website my images load, but when viewing one specific page, I cant get any images to load using already working code…

Python: Write nested list objects to csv file

Im trying to write data from a list of lists to a csv file. This is a simplified version of what I haveclass Point(object): def __init__(self, weight, height):self.weight = weightself.height = heightde…

Entire module is not detected by __init__.py

I have a relatively small python program which is setup like thisRoot--Network--DTOLots of py files which contain classes.Other py files in the projectBoth in the Network and the DTO folder there is an…

Python output to terminal during ssh login

I have been looking everywhere for this and have not found a solution. I am using python 2.5.1 on an Apple iPod and I am trying to connect to a host with SSH. First I start off with import os. Next I o…

Unable to find SIFT or xfeatures2d in OpenCV Python [duplicate]

This question already has answers here:PyCharm: Installation of non-free OpenCV modules for operations like SIFT, SURF(2 answers)Closed 6 years ago.I recently switch back to python for facial detection…

Django paginate for django 2

I need to use pagination to a Django list but I couldnt find any help online,, only old docs from Django version 1.3 here are my files : views.pydef home(request):all_dress = Item.objects.all().filter(…