Python 在dataframe中使用for循环对列进行迭代
我试图从CSV文件中读取一个数据帧,并为数据帧中的每一列生成散点图。例如,我用Python 在dataframe中使用for循环对列进行迭代,python,pandas,dataframe,bokeh,Python,Pandas,Dataframe,Bokeh,我试图从CSV文件中读取一个数据帧,并为数据帧中的每一列生成散点图。例如,我用df=pandas.readcsv() 我想生成一个散点图,使用sample作为x值,以及每个列的面积 我将以下代码用于bokeh.plotting以手动绘制每列 import pandas from bokeh.plotting import figure, show df = pandas.read_csv("data.csv") p = figure(x_axis_label='Sample', y_axis_
df=pandas.readcsv()
我想生成一个散点图,使用sample作为x值,以及每个列的面积
我将以下代码用于bokeh.plotting以手动绘制每列
import pandas
from bokeh.plotting import figure, show
df = pandas.read_csv("data.csv")
p = figure(x_axis_label='Sample', y_axis_label='Peak Area', x_range=sorted(set(df['Sample'])))
p.scatter(df['Sample'], df['AMP'])
show(p)
这将成功生成散点图,但我想创建一个循环来为每列生成散点图。在我的完整数据集中,我有500多列要绘制
我参考了使用df.iteritems和df.itertuples迭代数据帧的参考资料,但我不确定如何获得所需的输出
我尝试了以下方法:
for index, row in df.iteritems():
p = figure()
p.scatter(df['Sample'], df[row])
show(p)
我马上就出错了:
raise KeyError(“%s”不在索引“%objarr[mask]KeyError:”['1A''1B'
“2A”“2B']不在索引中
有什么指导吗?提前谢谢。
iteritems
迭代列,而不是行。但真正的问题是当您尝试df[row]
而不是df[index]
时。我会将措辞切换到列,然后执行以下操作:
for colname, col in df.iteritems():
p = figure()
p.scatter(df['Sample'], df[colname])
show(p)
for colname, col in df.iteritems():
p = figure()
p.scatter(df['Sample'], df[colname])
show(p)