Python 使用熊猫打印包含列表的列
我有一个数据框(df),包含几列,其中两列在每行中存储一个列表:Python 使用熊猫打印包含列表的列,python,list,pandas,plot,dataframe,Python,List,Pandas,Plot,Dataframe,我有一个数据框(df),包含几列,其中两列在每行中存储一个列表: 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.011491
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
功能将这些列表绘制为条形图
使用
我可以绘制每个列表的第一个元素。然而,尝试
df.list1.plot(kind='bar', width=0.9, ax=bar_ax)
导致以下错误:
Empty 'DataFrame': no numeric data to plot
我想做的是,(1)将两个列表绘制成一个单独的绘图,如下所示:
df[['list1','list2']].plot(kind='bar', width=0.9, ax=bar_ax)
和(2)也只将每个列表的第一个元素绘制成一个条形图,我可以这样做:
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')
但是,这会导致条形图相互重叠(而不是堆叠!)-我希望将它们分组。示例:
df = pd.DataFrame({'list1':[[ 0.09173306, 0.12331911, 0.20057651], [ 0.03861522, 0.10524985],[ 0.02124905, 0.01149118 ]],
'list2':[[0.3128322, 0.27153913], [0.37265687, 0.48347806], [0.04348405, 0.17057435, 0.37838683, 0.37481453]]},
index=['A','D','E'])
第一个解决方案:
import matplotlib.pyplot as plt
df.list1.apply(lambda x: pd.Series(x)).plot(kind='bar', width=0.9)
plt.show()
带堆栈的第二个解决方案:
import matplotlib.pyplot as plt
df.list1.apply(lambda x: pd.Series(x)).plot(kind='bar', width=0.9)
plt.show()
我认为您首先需要通过使用构造函数将列表
转换为系列
来重塑数据
然后他们一起:
df = pd.concat([dfL1, dfL2], axis=1, keys=('list1','list2'))
print (df)
list1 list2
A 0 0.091733 0.312832
1 0.123319 0.271539
2 0.200577 NaN
D 0 0.038615 0.372657
1 0.105250 0.483478
E 0 0.021249 0.043484
1 0.011491 0.170574
2 NaN 0.378387
3 NaN 0.374815
最后:
考虑此
DF
包含如下列表所示的值:
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
,使列名显示为索引,并使列表中的各个值显示为各个系列对象
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[['list1','list2']].applymap(lambda x: x[0]).plot.bar(rot=0, color=list('br'))
我还使用了您基于concat的方法,这对于绘制更多列表非常有帮助,我相信这是其他用户的偏好问题。再次感谢。
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'))
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))
df[['list1','list2']].applymap(lambda x: x[0]).plot.bar(rot=0, color=list('br'))