【发布时间】:2017-06-18 21:54:28
【问题描述】:
我有一个组织成 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 并尝试了for variable in subset[x].idx[1:],但它给了我AttributeError: 'Series' object has no attribute 'idx'。
有没有更好的方法来做到这一点?
【问题讨论】:
-
我无法理解您的数据框的外观。您能否提供具有预想结构的 Dataframe 的打印输出,并清楚说明这与
x、y1等有何关系? -
@ImportanceOfBeingErnest 我正在使用世界银行的数据集。我更新了帖子以包含更多信息。
标签: python python-3.x pandas matplotlib seaborn