我有一个数据集组织成 Pandas 数据框。
这是数据的一个小示例:
x142_2012 x126_2012 x156_2012 x167_2012 x1_2012 x243_2012
0 690.842629 0.005029 51.600000 5.454545 43.000000 27.700000
1 4247.485437 5.062739 95.400000 54.655959 100.000000 15.700000
2 5583.616160 NaN 84.900000 15.228027 100.000000 31.600000
3 NaN NaN 100.000000 NaN 59.328910 NaN
4 39666.369210 34.335120 100.000000 86.434425 100.000000 50.000000
5 5531.776299 NaN 47.800000 16.937210 37.000000 34.100000
6 13525.616220 14.674017 97.900000 58.000000 90.875440 10.500000
7 7465.145864 3.196932 85.417850 29.954302 86.270751 14.872018
8 14357.411590 12.530952 98.600000 55.800000 99.800000 37.400000
9 3565.517575 7.142042 99.700000 37.500000 100.000000 10.700000
10 NaN NaN 98.100000 74.000000 90.875440 NaN
我想构建一堆散点图,分别将变量 x142_2012 与其他变量进行比较。因此,我想迭代数据帧,同时跳过第一个条目。我试过这个
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
for variable in subset[1:]:
plt.figure()
scatterplot = sns.regplot(x="x142_2012", y=variable, fit_reg=False, data=subset)
但它不是输出 5 个散点图(x/y1、x/y2、x/y3、x/y4、x/y5
),而是输出 6 个散点图,第一个是 x/x
.
我正在解决这个问题:
for variable in subset:
if variable == "x142_2012":
continue
plt.figure()
scatterplot = sns.regplot(x="x142_2012", y=variable, fit_reg=False, data=subset)
但我觉得它不太优雅。我看了Efficient way to do pandas operation and skip row并尝试在子集[x].idx[1:]
中查找变量,但它给了我AttributeError:'Series'对象没有属性'idx'
。
有更好的方法吗?
最佳答案
不要使用subset[1:]
,而是使用subset.columns[1:]
关于python - 迭代 Pandas 数据帧,同时跳过第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41991855/