I have this data set,
and I want hash the zone
into colors such that
3: Yellow c will be yellow for 3 , i+=1
4: Green c will be green for 4, i+=1
2: Orange :
1: Maroon :
Here's the code,
colors={1:'Maroon',2:'Orange',3:'Yellow',4:'Green',5:'darkpurple',6:'Gray'}
for i in df_station.index[:25]:
c=str(df_station.loc[colors,'Zone'])
But, gives me an error like
1 4
2 2
3 3
4 3
5 3,4,5,6
6 3,4,5,6
Name: Zone, dtype: object
Please help me, how can I do this? Looks quite simple, but I am not able to find a correct syntax to do this
First of all, let me reproduce
Sample df_station (DataFrame)
mentioned in your Query. then we can move on to Solution
. Code
of DataFrame
Reproduction
was stated below:-
# Import all-important Libraries
import pandas as pd
# Reproduction of Sample 'df_station' DataFrame
df_station = pd.DataFrame({
'Station': ['Abbey Road', 'Abbey Wood', 'Acton Central', 'Acton Main Line', 'Acton Town'],
'OS X': [539081, 547297, 520613, 520296, 519457],
'OS Y': [183352, 179002, 180299, 181196, 179639],
'Latitude': [51.521952, 51.490784, 51.508758, 51.516887, 51.503071],
'Longitude': [0.003723, 0.120271, -0.263430, -0.267690, -0.280303],
'Zone': [3, 4, 2, 3, 3],
'Postcode': ['E15 3NB', 'SE2 9RH', 'W3 6BH', 'W3 9EH', 'W3 8HN']
})
# Print records of 'df_station' DataFrame
df_station
# Output of above Cell:-
Station OS X OS Y Latitude Longitude Zone Postcode
0 Abbey Road 539081 183352 51.521952 0.003723 3 E15 3NB
1 Abbey Wood 547297 179002 51.490784 0.120271 4 SE2 9RH
2 Acton Central 520613 180299 51.508758 -0.263430 2 W3 6BH
3 Acton Main Line 520296 181196 51.516887 -0.267690 3 W3 9EH
4 Acton Town 519457 179639 51.503071 -0.280303 3 W3 8HN
You can create a function
that will have the Conversion
functionality. Code
for the same approach was stated below:-
# Declaration of 'hash_zone_color' Function for Conversion of 'Zone Number' into 'Zone Colors'
def hash_zone_color(zone_color):
if zone_color == 1:
return 'Maroon'
elif zone_color == 2:
return 'Orange'
elif zone_color == 3:
return 'Yellow'
elif zone_color == 4:
return 'Green'
elif zone_color == 5:
return 'Dark Purple'
elif zone_color == 6:
return 'Gray'
else:
pass
# Initialization of 'hash_zone_color' function for the Conversion
df_station['Zone'] = df_station['Zone'].apply(hash_zone_color)
# Print Updated Records
df_station
As you can see, we have used pandas.DataFrame.apply()
for parsing argument
of Hash Number of Zone
to function
. So, that we can Map
Zone Colors
. Output
of above code was given below:-
# Output of above cell:-
Station OS X OS Y Latitude Longitude Zone Postcode
0 Abbey Road 539081 183352 51.521952 0.003723 Yellow E15 3NB
1 Abbey Wood 547297 179002 51.490784 0.120271 Green SE2 9RH
2 Acton Central 520613 180299 51.508758 -0.263430 Orange W3 6BH
3 Acton Main Line 520296 181196 51.516887 -0.267690 Yellow W3 9EH
4 Acton Town 519457 179639 51.503071 -0.280303 Yellow W3 8HN
To Learn more about
pandas.DataFrame.apply()
:- Click Here
Hope this Solution helps you.
Just map colors
dictionary to Series.
df['Zone'] = df['Zone'].map(colors)
If values in Zone
contains value not in the key of colors
. You can use dict.get(key, default)
to assign a default value if key is not found.
df['Zone'] = df['Zone'].map(lambda x: colors.get(x, 'Unknown Color'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.