Python 在跳过第一行的同时迭代数据帧
我将一个数据集组织成一个数据框架 以下是数据的一个小示例:Python 在跳过第一行的同时迭代数据帧,python,python-3.x,pandas,matplotlib,seaborn,Python,Python 3.x,Pandas,Matplotlib,Seaborn,我将一个数据集组织成一个数据框架 以下是数据的一个小示例: 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
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)
但我觉得它不太优雅。我查看并尝试在子集[x]中查找变量。idx[1:],但它给了我AttributeError:“Series”对象没有属性“idx”
有更好的方法吗?子集[1:]选择除第一行以外的所有行,生成的数据帧仍有六列
您可以做的是迭代数据帧的列并省略第一列:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# generate some data
a = np.random.rand(10,6)
a[:,0]= np.arange(10)
df = pd.DataFrame(a, columns=[l for l in "xabcde"])
#print df
#plot
for col in df.columns[1:]:
plt.figure()
scatterplot = sns.regplot(x="x", y=col, fit_reg=False, data=df)
plt.show()
子集[1:]选择除第一行以外的所有行,生成的数据帧仍有六列
您可以做的是迭代数据帧的列并省略第一列:
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# generate some data
a = np.random.rand(10,6)
a[:,0]= np.arange(10)
df = pd.DataFrame(a, columns=[l for l in "xabcde"])
#print df
#plot
for col in df.columns[1:]:
plt.figure()
scatterplot = sns.regplot(x="x", y=col, fit_reg=False, data=df)
plt.show()
使用subset.columns[1://代替subset.columns[1://使用subset.columns[1://代替subset.columns[1://我无法理解数据帧的外观。你能提供一个具有预期结构的数据框的打印输出,并清楚地说明它与x、y1等的关系吗?@ImportanceOfBeingErnest我正在使用世界银行的数据集。我已经更新了帖子以包含更多信息。我在理解数据帧的外观时遇到问题。你能提供一个具有预期结构的数据框的打印输出,并清楚地说明它与x、y1等的关系吗?@ImportanceOfBeingErnest我正在使用世界银行的数据集。我已经更新了帖子,增加了一些信息。