Fill new column by following conditions listed in a dictionary [duplicate]
2024/11/18 7:30:56
I have the dictionary specifying the value the row should take if the conditions are met. The dictionary can be restructured if needed (or reshape it if you prefer while preserving the information in it)
Transform your dict_map into a Series of query string usable by query method:
sr = pd.Series({k1: ' & '.join([f"{k2} == '{v}'" for k2, v in d.items()])for k1, d in dict_map.items()})for v, q in sr.iteritems():df.loc[df.query(q).index, 'Status'] = v
Output:
>>> dfID Sex Pregnant Status
0 AB01 F Y Yes
1 AB02 M N N/A
2 AB03 M NaN N/A
3 AB04 NaN Y NaN
4 AB05 F NaN
5 AB06 F N No>>> sr
Yes Sex == 'F' & Pregnant == 'Y'
No Sex == 'F' & Pregnant == 'N'
N/A Sex == 'M'
Name: Status, dtype: object
Update
I have scenario in which the dictionary is like {'Yes': {'Sex': ('F', 'Female')}, 'No': {'Sex': 'M', 'Pregnant': 'N\A'}} how do I convert this into a series like above format? In english, Yes if sex is F or Female and No if sex is M and pregnant is N\A
Use isin when the value is a tuple:
dict_map2 = {'Yes': {'Sex': ('F', 'Female')},'No': {'Sex': 'M', 'Pregnant': 'N\A'}}sr = pd.Series({k1: ' & '.join((f"{k2} == '{v}'" if isinstance(v, str) else f"{k2}.isin({v})")for k2, v in d.items())for k1, d in dict_map2.items()})
print(sr)# Output:
Yes Sex.isin(('F', 'Female'))
No Sex == 'M' & Pregnant == 'N\A'
dtype: object
Im new to pandas. I have a large excel file, what I’m trying to do is split the data frame after manipulation into multiple excel workbooks. There is more or less 400 vendors and I would like each To …
I am newbie to Python. I have large file with repetitive string through the logsExample:abc
def
efg
gjk
abc
def
efg
gjk
abc
def
efg
gjk
abc
def
efg
gjkExpected Result--------------------Section1-------…
I have CSV example like this
ID,TASK1,TASK2,QUIZ1,QUIZ2
11061,50,75,50,78
11062,70,80,60,50
11063,60,75,77,79
11064,52,85,50,80
11065,70,85,50,80how do i get the Max, Min and Avg on specific Column?
i…
I need help in debugging. I just cant figure out why its not working as expected.The Code below should read data files (names are stored in all_files) in chunks of 6, arrange them in subplots (i,j indi…
I have a string:testString = """ My name is %s
and I am %s years old
and I live in %s"""I have code that finds these three strings that I want to input into testString. Ho…
I have a list of random numbers and I would like to get the greatest number using multiprocessing.
This is the code I used to generate the list:
import random
randomlist = []
for i in range(100000000):…
i am try to pullout intraday aapl stock data by yahoo. but there problem i facing with my program..import pandas as pd
import datetime
import urllib2
import matplotlib.pyplot as plt
get = http://chart…
I have a camera with these specs:full resolution 1280x1024
pixel size 0.0048mm
focal length 8 mmI need to detect a ball in this image. It is 4 meters away and its radius is 0.0373 meter.
How to convert…
A GPA, or Grade point Average, is calculated by summing the grade points earned in a student’s courses and then dividing by the total units. The grade points for an individual course are calculated by…