python - 将数字中的颜色散列到 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
请帮助我,我该怎么做?看起来很简单,但我找不到正确的语法来做到这一点
解决方案
首先,让我reproduce
Sample df_station (DataFrame)
在您的查询中提到。然后我们可以继续Solution
。Code
的DataFrame
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 argument
of Hash Number of Zone
to function
。所以,我们可以Map
Zone Colors
。Output
上面的代码如下: -
# 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()
:-点击这里
希望此解决方案对您有所帮助。
推荐阅读
- excel - 如何上传excel文件并验证列是否有效
- javascript - 如何在页面上的第 12 行中对卡片块 4 进行分页?
- json - 如何使用 jq 将子值聚合到数组中?
- apache-spark - 如何“强制”Spark 上的 Hive 使用 Map Join?
- docker - docker COPY 不在 C 盘时全部失败
- css - 如何使用带有字母间距的线性渐变?
- javascript - 为什么 1+[ ] 在 JavaScript 中返回“1”?
- java - 在安装时选择 Android 应用程序的颜色
- arrays - 冒号分隔的输出php数组
- python - 从数据框转换为文本文件不会分隔行