I have a Dataframe with the column 'all_maxs'
that could have a list of different values.
JavaScript
x
14
14
1
c all_maxs
2
38 50804.6 [50883.3]
3
39 50743.9 [50883.3]
4
40 50649.9 [50883.3]
5
41 50508.3 [50883.3]
6
42 50577.6 [50883.3]
7
43 50703.0 [50883.3]
8
44 50793.7 [50883.3]
9
45 50647.8 [50883.3, 50813.1]
10
46 50732.8 [50883.3, 50813.1]
11
47 50673.2 [50883.3, 50813.1]
12
13
df.plot(y='c')
14
Current Result
I need to plot column 'c'
, and the values of column 'all_maxs'
that should be horizontal lines.
Expected Result
Advertisement
Answer
- Verify the
'all_maxs'
values arelist
type. - Extract values from the lists and plot them as horizontal lines.
df = df.dropna()
if there are anyNaN
Imports and DataFrame
- Convert the
'all_maxs'
column type fromstr
tolist
if needed, usingast.liter_eval
JavaScript
1
31
31
1
import pandas as pd
2
from ast import literal_eval
3
4
data =
5
{38: {'all_maxs': '[50883.3]', 'c': 50804.6},
6
39: {'all_maxs': '[50883.3]', 'c': 50743.9},
7
40: {'all_maxs': '[50883.3]', 'c': 50649.9},
8
41: {'all_maxs': '[50883.3]', 'c': 50508.3},
9
42: {'all_maxs': '[50883.3]', 'c': 50577.6},
10
43: {'all_maxs': '[50883.3]', 'c': 50703.0},
11
44: {'all_maxs': '[50883.3]', 'c': 50793.7},
12
45: {'all_maxs': '[50883.3, 50813.1]', 'c': 50647.8},
13
46: {'all_maxs': '[50883.3, 50813.1]', 'c': 50732.8},
14
47: {'all_maxs': '[50883.3, 50813.1]', 'c': 50673.2}}
15
16
df = pd.DataFrame.from_dict(data, orient='index')
17
18
# reorder the columns to match the OP
19
df = df[['c', 'all_maxs']]
20
21
# print a value from all_maxs to see the type
22
>>> print(type(df.loc[38, 'all_maxs']))
23
str
24
25
# currently the all_max values are strings, which must be converted to list type
26
df.all_maxs = df.all_maxs.apply(literal_eval)
27
28
# print a value from all_maxs to see the type
29
>>> print(type(df.loc[38, 'all_maxs']))
30
list
31
Plot
- Plot the dataframe directly with
pandas.DataFrame.plot
xticks=df.index
will make an xtick for every value in the index, but if there are many values crowding the x-axis, remove this parameter.
- Use
matplotlib.pyplot.hlines
, which will accept a list of values, to plot the unique values from'all_max'
as horizontal lines.- Use
pandas.DataFrame.explode
to remove all the values from lists, and then drop duplicates with.drop_duplicates
y=
will be the remaining values in the'all_maxs'
columnxmin=
will be the remaining index valuesxmax=
will be the max value in the index plotted fromdf
- Use
JavaScript
1
8
1
ax = df.plot(y='c', legend=False, figsize=(8, 5), xticks=df.index)
2
3
# extract all the values from all_maxs, drop the duplicates
4
all_maxs = df.all_maxs.explode().drop_duplicates().to_frame()
5
6
# add the horizontal lines
7
ax.hlines(y=all_maxs.all_maxs, xmin=all_maxs.index, xmax=df.index.max(), color='k')
8