首页 > 解决方案 > 将数字中的颜色散列到 DataFrame-Python 中

问题描述

我有这个数据集,

在此处输入图像描述

我想把它散列zone成颜色,这样

3: Yellow    c will be yellow for 3 , i+=1 
4: Green     c will be green for 4, i+=1 
2: Orange         :
1: Maroon         :

这是代码,

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'])

但是,给了我一个错误,比如

1          4
2          2
3          3
4          3
5    3,4,5,6
6    3,4,5,6
Name: Zone, dtype: object

请帮助我,我该怎么做?看起来很简单,但我找不到正确的语法来做到这一点

标签: pythonpandas

解决方案


首先,让我reproduce Sample df_station (DataFrame)在您的查询中提到。然后我们可以继续SolutionCodeDataFrame Reproduction如下所述:-

# 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

适当的解决方案:-

您可以创建一个function具有该Conversion功能的。Code对于相同的方法如下所述:-

# 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

如您所见,我们使用pandas.DataFrame.apply()parsing argumentof Hash Number of Zoneto function。所以,我们可以Map Zone ColorsOutput上面的代码如下: -

# 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

了解更多pandas.DataFrame.apply():-点击这里

希望此解决方案对您有所帮助。


推荐阅读