I work with two different pandas dataframes:
dataframe1:
Year State EMW
0 1968 Alabama 8.55
1 1968 Alaska 15.61
2 1968 Arizona 8.55
3 1968 Arkansas 8.55
4 1968 California 12.26
... ... ... ...
2857 2020 Virginia 7.25
2858 2020 Washington 13.50
2859 2020 West Virginia 8.75
2860 2020 Wisconsin 7.25
2861 2020 Wyoming 7.25
and dataframe2:
NAME STATUS ISO ANSI1 ANSI2 USPS
0 Alabama State US-AL AL 1 AL
1 Alaska State US-AK AK 2 AK
2 Arizona State US-AZ AZ 4 AZ
3 Arkansas State US-AR AR 5 AR
4 California State US-CA CA 6 CA
5 Colorado State US-CO CO 8 CO
6 Connecticut State US-CT CT 9 CT
7 Delaware State US-DE DE 10 DE
8 District of Columbia Federal district US-DC DC 11 q
9 Florida State US-FL FL 12 FL
... ... ... ... ... ... ...
What I'm Trying to do:
Replace all values in the 'State' column in dataframe1 with their equivalent ANSI1 code from dataframe2.
So basically, I'm looking to have a result such as this:
Alabama -> AL
Alaska -> AK
Arizona -> AZ
and so on.
For some reason, nothing I've tried so far works.
What I've tried:
- A single line for loop
[dataframe1.replace({'State' : {dataframe2.loc[i]['NAME'] : dataframe2.loc[i][ANSI1']}},inplace = True) for i in range(0, len(dataframe2))]
- An equivalent nested loop structure:
for state_name in pd.unique(dataframe1['State']):
for ansi_name in dataframe2['ANSI1']:
if ansi_name == state_name :
dataframe1.replace({'State' : { state_name : ansi_name }}, inplace = True)
Note I suspected I might be trying to compare different types so I tried:
dataframe1.replace({'State' : {'Alabama' : 'AL'}}, inplace=True)
and sure enough, it worked.
EDIT:
Creating a dictionary with
dState = dict(df1[['NAME', 'ANSI1']].values)
produces a dictionary where the values are as follows:
{'\xa0Alabama': 'AL', '\xa0Alaska': 'AK', '\xa0Arizona': 'AZ', '\xa0Arkansas': 'AR', '\xa0California': 'CA', '\x
a0Colorado': 'CO', '\xa0Connecticut': 'CT', '\xa0Delaware': 'DE', ' District of Columbia': 'DC', ' Florida': 'FL
', '\xa0Georgia': 'GA', '\xa0Hawaii': 'HI', '\xa0Idaho': 'ID', '\xa0Illinois': 'IL', '\xa0Indiana': 'IN', '\xa0I
owa': 'IA', '\xa0Kansas': 'KS', '\xa0Kentucky': 'KY', '\xa0Louisiana': 'LA', '\xa0Maine': 'ME', '\xa0Maryland':
'MD', '\xa0Massachusetts': 'MA', '\xa0Michigan': 'MI', '\xa0Minnesota': 'MN', '\xa0Mississippi': 'MS', '\xa0Miss
ouri': 'MO', '\xa0Montana': 'MT', '\xa0Nebraska': 'NE', '\xa0Nevada': 'NV', '\xa0New Hampshire': 'NH', '\xa0New
Jersey': 'NJ', '\xa0New Mexico': 'NM', '\xa0New York': 'NY', '\xa0North Carolina': 'NC', '\xa0North Dakota': 'ND
', '\xa0Ohio': 'OH', '\xa0Oklahoma': 'OK', '\xa0Oregon': 'OR', '\xa0Pennsylvania': 'PA', '\xa0Rhode Island': 'RI
', '\xa0South Carolina': 'SC', '\xa0South Dakota': 'SD', '\xa0Tennessee': 'TN', '\xa0Texas': 'TX', '\xa0Utah': '
UT', '\xa0Vermont': 'VT', '\xa0Virginia': 'VA', '\xa0Washington': 'WA', '\xa0West Virginia': 'WV', '\xa0Wisconsi
n': 'WI', '\xa0Wyoming': 'WY', ' Puerto Rico': 'PR', ' U.S. Virgin Islands': 'VI', ' Guam': 'GU', ' Northern Mar
iana Islands': 'MP', ' American Samoa': 'AS'}
So it makes sense now that I couldn't get anywhere by comparing them to the values from df1['State']
I am now starting to suspect that I may have missed something in the encoding of the csv I import df2 from.
NAMEandANSI1from DataFrame2. Then do a map on DataFrame1 using the dictionary. That will solve. Let me write up the code and share shortly\xa0and leading spaces or you can do that as a post process\xa0only shows up when I print out the dictionary and not when I print out the dataframe itself, so I'm not sure as to at which point I should sanitize it.df2['NAME'] = df2.NAME.str.replace(r'\xa0|^ ',''). It will remove all leading spaces and\xa0