在 Pandas 数据框中使用 for 循环迭代列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38152686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:30:34  来源:igfitidea点击:

Iterating over columns with for loops in pandas dataframe

pythonpandasdataframebokeh

提问by JeremyD

I am trying to take a dataframe read in from CSV file, and generate scatter plots for each column within the dataframe. For example, I have read in the following with df=pandas.readcsv()

我正在尝试从 CSV 文件中读取一个数据框,并为数据框中的每一列生成散点图。例如,我阅读了以下内容df=pandas.readcsv()

Sample    AMP    ADP    ATP
1A        239847 239084 987374
1B        245098 241210 988950
2A        238759 200554 921032
2B        230029 215408 899804

I would like to generate a scatter plot using sample as the x values, and the areas for each of the columns.

我想使用样本作为 x 值和每一列的面积来生成散点图。

I am using the following code with bokeh.plotting to plot each column manually

我使用以下代码与 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)

This generates scatter plots successfully, but I would like to create a loop to generate a scatter plot for each column. In my full dataset, I have over 500 columns I would like to plot.

这会成功生成散点图,但我想创建一个循环来为每一列生成散点图。在我的完整数据集中,我想要绘制超过 500 列。

I have followed references for using df.iteritems and df.itertuples for iterating through dataframes, but I'm not sure how to get the output I want.

我遵循了使用 df.iteritems 和 df.itertuples 迭代数据帧的参考资料,但我不确定如何获得我想要的输出。

I have tried the following:

我尝试了以下方法:

for index, row in df.iteritems():
    p = figure()
    p.scatter(df['Sample'], df[row])
    show(p)

I hit an error right away:

我马上就遇到了一个错误:

raise KeyError('%s not in index' % objarr[mask] KeyError: "['1A' '1B' '2A' '2B'] not in index

raise KeyError('%s not in index' % objarr[mask] KeyError: "['1A' '1B' '2A' '2B'] 不在索引中

Any guidance? Thanks in advance.

任何指导?提前致谢。

回答by piRSquared

iteritemsiterates over columns, not rows. But your real problem is when you are trying to df[row]instead of df[index]. I'd switch wording to columns and do this:

iteritems迭代列,而不是行。但你真正的问题是当你试图df[row]代替df[index]. 我会将措辞切换到列并执行以下操作:

for colname, col in df.iteritems():
p = figure()
p.scatter(df['Sample'], df[colname])
show(p)