Python 在数据框中打印列表
考虑以下数据帧示例:Python 在数据框中打印列表,python,list,pandas,dataframe,plot,Python,List,Pandas,Dataframe,Plot,考虑以下数据帧示例: df = pd.DataFrame({'date':['12-01', '12-02', '12-03'],'list1': np.random.randint(0, 10, (3,5)).tolist(), 'list2': np.random.randint(0, 10, (3,5)).tolist()}, index=list('ABC')) 我希望以这样的方式在数据框中绘制列表:在我的图形中有3行:
df = pd.DataFrame({'date':['12-01', '12-02', '12-03'],'list1': np.random.randint(0, 10, (3,5)).tolist(),
'list2': np.random.randint(0, 10, (3,5)).tolist()},
index=list('ABC'))
我希望以这样的方式在数据框中绘制列表:在我的图形中有3行:a、B和C,每行的“list1”作为x值,“list2”作为y值
我开始尝试直接绘制列,但以下返回错误:
In: df.iloc[0].plot(x='list1', y='list2')
Out: TypeError: Empty 'DataFrame': no numeric data to plot
我可以迭代数据帧的行,并将列表附加到一个新的列表中以绘制它们,但我觉得这违背了将数据存储在数据帧中的目的(我有附加列和辅助数据),我确信有更好的方法来实现这一点
我在每个数据帧中有大约20个条目(从较大的数据帧分组),主要目的是直观地检查值(仪器输出)是否在某个范围内。为什么不迭代行并绘制值
for i, row in df.iterrows():
plt.plot(row['list1'], row['list2'])
您正在使用
plot方法,该方法需要数据帧输入。如果直接使用matplotlib
,则可以这样打印:
plt.plot(df.iloc[0]['list1'],df.iloc[0]['list2'])