I have the following dataframe:
df = pd.DataFrame([['Person1', 'CT', 2017],
['Person2', 'FL', 2017],
['Person3', 'TX', 2017],
['Person1', 'TX', 2016]], columns=['Name', 'State', 'Year'])
And two mapping tables below:
state_map = {'CT': 'Connecticut', 'FL': 'Florida', 'TX':'Texas'}
state_map2 = {'CT': 'ABC-CT', 'FL': 'BBC-Florida', 'TX':'CDA-TX'}
Here is what the data looks like:
Name State Year
0 Person1 CT 2017
1 Person2 FL 2017
2 Person3 TX 2017
3 Person1 TX 2016
I would like to find a way to add a new column with values mapped using an if condition that determines whether to use values mapped from state_map or state_map2. So if df[df['Name']=='Person1'] then use state_map else use state_map2.
The final output should look like this:
Name State Year New_State_Name
0 Person1 CT 2017 Connecticut
1 Person2 FL 2017 BBC-Florida
2 Person3 TX 2017 CDA-TX
3 Person1 TX 2016 Texas
I tried the following code but it didn't work.
df['New_State_Name'] = [state_map[x] if df[df['Name'] == 'Person1'] else
state_map2[x] for x in df['State']]
I got an error that says:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty,
a.bool(), a.item(), a.any() or a.all().
You can extract a column of pandas DataFrame based on another value by using the DataFrame. query() method. The query() is used to query the columns of a DataFrame with a boolean expression. The blow example returns a Courses column where the Fee column value matches with 25000.
You can use drop , isin and any . drop the target column to have a df with your A , B , C columns only. check if the values isin the target column. and check if any hits are present.
pandas. map() is used to map values from two series having one column same. For mapping two series, the last column of the first series should be same as index column of the second series, also the values should be unique.
use df. replace({"Duration": dict_duration},inplace=True) to remap none or NaN values in pandas DataFrame with Dictionary values. To remap None / NaN values of the 'Duration ' column by their respective codes using the df. replace() function.
Use np.where
:
df['New_State_Name'] = np.where(df['Name']=='Person1',df['State'].map(state_map),df['State'].map(state_map2))
Output:
Name State Year New_State_Name
0 Person1 CT 2017 Connecticut
1 Person2 FL 2017 BBC-Florida
2 Person3 TX 2017 CDA-TX
3 Person1 TX 2016 Texas
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With