Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas map column data based on value from another column using if to determine which dict to use

Tags:

python

pandas

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().
like image 326
Tony Avatar asked Jul 26 '17 17:07

Tony


People also ask

How do you get a value from a column based on another column pandas?

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.

How do you see if values in one column are in another pandas?

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.

Which function is used for index and column mapping in pandas?

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.

How do you remap values in pandas DataFrame column with a dictionary and preserve NANS?

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.


1 Answers

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
like image 88
Scott Boston Avatar answered Oct 21 '22 22:10

Scott Boston