python - 如何将列表的数据框列绘制为水平线
问题描述
我有一个数据框,其中的列'all_maxs'
可能包含不同值的列表。
c all_maxs
38 50804.6 [50883.3]
39 50743.9 [50883.3]
40 50649.9 [50883.3]
41 50508.3 [50883.3]
42 50577.6 [50883.3]
43 50703.0 [50883.3]
44 50793.7 [50883.3]
45 50647.8 [50883.3, 50813.1]
46 50732.8 [50883.3, 50813.1]
47 50673.2 [50883.3, 50813.1]
df.plot(y='c')
当前结果
我需要绘制 column 'c'
,并且 column 的值'all_maxs'
应该是水平线。
预期结果
解决方案
- 验证
'all_maxs'
值是list
类型。 - 从列表中提取值并将它们绘制为水平线。
df = df.dropna()
如果有的话NaN
导入和数据框
- 如果需要,将
'all_maxs'
列类型从str
转换为,使用list
ast.liter_eval
import pandas as pd
from ast import literal_eval
data =\
{38: {'all_maxs': '[50883.3]', 'c': 50804.6},
39: {'all_maxs': '[50883.3]', 'c': 50743.9},
40: {'all_maxs': '[50883.3]', 'c': 50649.9},
41: {'all_maxs': '[50883.3]', 'c': 50508.3},
42: {'all_maxs': '[50883.3]', 'c': 50577.6},
43: {'all_maxs': '[50883.3]', 'c': 50703.0},
44: {'all_maxs': '[50883.3]', 'c': 50793.7},
45: {'all_maxs': '[50883.3, 50813.1]', 'c': 50647.8},
46: {'all_maxs': '[50883.3, 50813.1]', 'c': 50732.8},
47: {'all_maxs': '[50883.3, 50813.1]', 'c': 50673.2}}
df = pd.DataFrame.from_dict(data, orient='index')
# reorder the columns to match the OP
df = df[['c', 'all_maxs']]
# print a value from all_maxs to see the type
>>> print(type(df.loc[38, 'all_maxs']))
str
# currently the all_max values are strings, which must be converted to list type
df.all_maxs = df.all_maxs.apply(literal_eval)
# print a value from all_maxs to see the type
>>> print(type(df.loc[38, 'all_maxs']))
list
阴谋
- 直接绘制数据框
pandas.DataFrame.plot
xticks=df.index
将为索引中的每个值创建一个 xtick,但如果有很多值拥挤在 x 轴上,请删除此参数。
- 使用
matplotlib.pyplot.hlines
将接受值列表的 ,将唯一值绘制'all_max'
为水平线。- 用于
pandas.DataFrame.explode
从列表中删除所有值,然后删除重复项.drop_duplicates
y=
将是'all_maxs'
列中的剩余值xmin=
将是剩余的索引值xmax=
将是从绘制的索引中的最大值df
- 用于
ax = df.plot(y='c', legend=False, figsize=(8, 5), xticks=df.index)
# extract all the values from all_maxs, drop the duplicates
all_maxs = df.all_maxs.explode().drop_duplicates().to_frame()
# add the horizontal lines
ax.hlines(y=all_maxs.all_maxs, xmin=all_maxs.index, xmax=df.index.max(), color='k')
推荐阅读
- ios - 水平渐变不适用于动态宽度内容
- ionic-framework - 表情符号没有从离子中的文本输入中发送
- python - 如何合并不同嵌套级别的嵌套字典?
- angularjs - 在学习 Angular 4-6 之前需要学习 AngularJS 吗?
- php - 忽略 PHP 函数中的本地化转换
- qt - Qt:在 SQLite 数据库上捕获外部更改
- html - 垂直对齐:在多行文本之后
- angular - 如何在 Angular 中执行代码之前等待 API 响应
- objective-c - 目标-c:当数据来自字符串时,NSJSONSerialization 失败
- java - 通过 MessageMapping 验证 spring websocket