Home > Software design >  How to combine two columns in pandas dataframe and set values to them?
How to combine two columns in pandas dataframe and set values to them?

Time:04-30

I have two columns in pandas dataframe Latitude and Longitude. I am trying two combine them in single column LOCATION. If we see the data there are only two locations are present.

Input:

LATITUDE    LONGITUDE
29.14290089 -100.73896686
29.142901   -100.738967
29.14290089 -100.73896686
29.142901   -100.738967
29.14290089 -100.73896686
29.142901   -100.738967
29.14290089 -100.73896686

Expected Output:

LOCATION
Loc_1
Loc_2
Loc_1
Loc_2
Loc_1
Loc_2
Loc_1

CodePudding user response:

Use:

df['LOCATION'] = pd.factorize(df[['LATITUDE','LONGITUDE']].apply(tuple, 1))[0]   1

df['LOCATION'] = 'Loc_'   df['LOCATION'].astype(str)
print (df)
    LATITUDE   LONGITUDE LOCATION
0  29.142901 -100.738967    Loc_1
1  29.142901 -100.738967    Loc_2
2  29.142901 -100.738967    Loc_1
3  29.142901 -100.738967    Loc_2
4  29.142901 -100.738967    Loc_1
5  29.142901 -100.738967    Loc_2
6  29.142901 -100.738967    Loc_1

CodePudding user response:

Here is another solution:

locs = {(29.14290089, -100.73896686) : "Loc_1",
        (29.142901, -100.738967) : "Loc_2"}

out = df.apply(tuple, axis=1).map(locs)
print(out)

0    Loc_1
1    Loc_2
2    Loc_1
3    Loc_2
4    Loc_1
5    Loc_2
6    Loc_1
dtype: object

If you want it as new column in your df just do:

df['LOCATION'] = df.apply(tuple, axis=1).map(locs)
  • Related