0

I have a dataframe with lots of rows

0  val1
1  val2
2  val3
3  val3
4  val4
5  nan
6  nan
7  val7

I drop the na values I am left with:

0  val1
1  val2
2  val3
3  val3
4  val4
7  val7

However, now if I iterate over this doing something like:

for i in range(df.shape[0]):
    row = df.at[1, 'colname']

I get an index error. I need to reindex it so it becomes like:

0  val1
1  val2
2  val3
3  val3
4  val4
5  val7

What is the best way to do this?

Thanks

More Than Five
  • 9,959
  • 21
  • 77
  • 127
  • 3
    `df.reset_index(drop=True, inplace=True)` – sacuL Mar 25 '18 at 15:12
  • 3
    I think need https://stackoverflow.com/q/20490274/2901002 – jezrael Mar 25 '18 at 15:14
  • 2
    Possible duplicate of [How to reset index in a pandas data frame?](https://stackoverflow.com/questions/20490274/how-to-reset-index-in-a-pandas-data-frame) – bunji Mar 25 '18 at 15:14
  • If you're going to iterate through your dataframe, it might also be useful to look into `df.iterrows()` – sacuL Mar 25 '18 at 15:37

1 Answers1

2

The answer depends on whether you need to preserve the old index or not. If you do not want to preserve it, use one of the solutions offered in the comments (e.g. df.reset_index(drop=True, inplace=True)) and your original code.

If you want to preserve the old index, then you have to use it correctly:

for i in df.index:
    row = df.loc[i, 'colname']
DYZ
  • 55,249
  • 10
  • 64
  • 93