2

In my dataframe, there are NaN values in some rows. I want to delete these rows. I solve it with dataframe.dropna(how='any'). The result looks like:

         date  time   open   hign    low  close  volume  turnover
2  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
3  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

I want to reindex the rows of my dataframe, so I run:

length = dataframe.dropna(how='any').shape[0]
dataframe1 = dataframe.index(range(length))

But dataframe1 still keeps the old index values, like:

          date  time   open   hign    low  close  volume  turnover
0         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
1         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
2  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
3  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

How can I make the number begin with 0 and delete the first two rows?

Desired result:

          date  time   open   hign    low  close  volume  turnover
0  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
1  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508
0

2 Answers 2

7

Reset the index and specify drop=True.

df = pd.DataFrame({'close': [nan, nan, 48.0, 48.25],
                   'date': [nan, nan, '2015-09-01', '2015-09-01'],
                   'hign': [nan, nan, 48.60, 48.33],
                   'low': [nan, nan, 48.0, 47.91],
                   'open': [nan, nan, 48.60, 47.91],
                   'time': [nan, nan, 931.0, 932.0],
                   'turnover': [nan, nan, 21741726.0, 7614508.0],
                   'volume': [nan, nan, 449700.0, 158500.0]})

>>> df
         date  time   open   hign    low  close  volume  turnover
0         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
1         NaN   NaN    NaN    NaN    NaN    NaN     NaN       NaN
2  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
3  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508

>>> df.dropna(how='any').reset_index(drop=True)
         date  time   open   hign    low  close  volume  turnover
0  2015-09-01   931  48.60  48.60  48.00  48.00  449700  21741726
1  2015-09-01   932  47.91  48.33  47.91  48.25  158500   7614508
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.That helps me a lot.
-1

Did you try the reindex functionality?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.