Greg Greg - 4 years ago 136
Python Question

Iterate over a Pandas dataframe while skipping the first row

I have a data set organized into a Pandas dataframe.

Here's a small example of the data:

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


I want to build a bunch of scatterplots which compare the variable x142_2012 with each other variable, individually. Therefore, I want to iterate over the dataframe, while skipping the first entry. I tried this

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)


but instead of outputting 5 scatterplots (
x/y1, x/y2, x/y3, x/y4, x/y5
), it output 6 scatterplots with the first being
x/x
.

I'm getting around the issue with this:

for variable in subset:
if variable == "x142_2012":
continue
plt.figure()
scatterplot = sns.regplot(x="x142_2012", y=variable, fit_reg=False, data=subset)


But I don't find it very elegant. I looked at Efficient way to do pandas operation and skip row and tried
for variable in subset[x].idx[1:]
but it gives me
AttributeError: 'Series' object has no attribute 'idx'
.

Is there a better way to do this?

Answer Source

Instead of subset[1:], use subset.columns[1:]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download