I have a dataframe of the following form:
import pandas as pd
df = pd.DataFrame({'t': [0, 1, 2, 3, 4, 5, 6],
'l': [["c", "d"], ["a", "b"], ["c", "d"], ["a", "b"], ["c", "d"], ["c", "d"], ["c", "d"]]})
The column l
consists of lists, where the list-entries can either be in the set {a,b,c,d}
. I want to plot the contents of l
for each value of t
in the following manner which basically shows which of the four possible values {a,b,c,d}
are acticated
at a time t
:
In order to create the above plot, what I did was to create the following dataframe based on df
above ( -1
is not activated, otherwise non-negative):
df_plot = pd.DataFrame({'t': [0, 1, 2, 3, 4,5,6],
'a': [-1, 0, -1, 0, -1,-1,-1],
'b': [-1, 1, -1, 1, -1,-1,-1],
'c': [2, -1, 2, -1, 2,2,2],
'd': [3, -1, 3, -1, 3,3,3]})
import numpy as np
ax = df_plot.plot(x="t", y=["a","b","c","d"],style='.', ylim=[-0.5,3.5], yticks=np.arange(0,3.1,1),legend=False)
labels = ["a","b","c","d"]
ax.set_yticklabels(labels)
This technically gives me what I want, however, I'd like to think that there is an easier and more professional way to plot this - is there a smarter way using one of Python's libraries?
How about something like this:
# Reshape dataframe
dff = df.l.apply(pd.Series).merge(df, right_index = True, left_index = True).drop(["l"], axis = 1).melt(id_vars = ['t'], value_name = "l").drop("variable", axis = 1)
# Plot dataframe
import matplotlib.pyplot as plt
plt.scatter(dff['t'], dff['l'])
# plt.grid(True)
More details about what is going on in the code i wrote can be found clicking this link : https://mikulskibartosz.name/how-to-split-a-list-inside-a-dataframe-cell-into-rows-in-pandas-9849d8ff2401
Note : it should work no matter how many items you have in the lists in column l .
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.