使用Pandas绘制包含列表的列 [英] Plotting a column containing lists using Pandas
问题描述
我有一个包含几列的数据框(df),其中两列在每一行中存储一个列表:
I have a dataframe (df) containing several columns and two of them store a list in each row:
Index list1 list2
A [ 0.09173306 0.12331911 0.20057651 ] [ 0.3128322 0.27153913 ]
D [ 0.03861522 0.10524985 ] [ 0.37265687 0.48347806 ]
E [ 0.02124905 0.01149118 ] [ 0.04348405 0.17057435 0.37838683 0.37481453 ]
我想使用pandas
内置plot
函数将这些列表绘制为条形图.
I would like to plot these lists as bar graphs using pandas
built-in plot
function.
使用
df.list1.apply(lambda x:x[0]).plot(kind='bar', width=0.9, ax=bar_ax)
我可以绘制每个列表的第一个元素.但是,尝试
I can plot the first element of each list. However, trying
df.list1.plot(kind='bar', width=0.9, ax=bar_ax)
导致以下错误:
Empty 'DataFrame': no numeric data to plot
我想做的是,(1)将两个列表都绘制到一个图中,如下所示:
What I would like to do is, (1) plot both list into one single plot like so:
df[['list1','list2']].plot(kind='bar', width=0.9, ax=bar_ax)
并且(2)还将每个列表的第一个元素仅绘制到一个条形图中,我可以这样做:
And (2) also plot the first elements of each list only into one single bar plot, which I can do like this:
df.list1.apply(lambda x:x[0]).plot(kind='bar', width=0.9, ax=bar_ax, color='blue')
df.list2.apply(lambda x:x[0]).plot(kind='bar', width=0.9, ax=bar_ax, color='red')
但是,这导致条形图相互重叠(不堆叠!)-我想将它们分组.
However, this results in the bars being plotted on top of each other (not stacked!) - I want to have them grouped.
推荐答案
考虑此DF
包含值的列表,如下所示:
Consider this DF
containing values as lists as shown:
np.random.seed(42)
df = pd.DataFrame({'list1': np.random.randint(0, 10, (5,2)).tolist(),
'list2': np.random.randint(0, 10, (5,3)).tolist()},
index=list('ABCDE'))
Q-1 将两个列表都绘制到一个图中:
取消堆叠DF
,以使列名显示为索引,并使列表中的各个值呈现给各个系列对象.
Unstack the DF
to make the column names appear as index and make individual values present in the list to individual series objects.
df_lists = df[['list1','list2']].unstack().apply(pd.Series)
df_lists.plot.bar(rot=0, cmap=plt.cm.jet, fontsize=8, width=0.7, figsize=(8,4))
Q-2 将每个列表的第一个元素仅绘制到一个单独的条形图中:
使用 DF.applymap
进行选择获取分组条形图所需列的第一个元素.
Use DF.applymap
to select first element of the required columns to obtain the grouped bar plot.
df[['list1','list2']].applymap(lambda x: x[0]).plot.bar(rot=0, color=list('br'))
这篇关于使用Pandas绘制包含列表的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!