0
$\begingroup$

I have read data from a json file, and the dataframe looks like:

   Col1  Col2  Col3
A:   1    Hi    7
B:   22   CC    1

I think it is because the file was a dictionary of dictionaries and I do not need the outer dictionary key, which is now showing as index identifier (A and B)

I need to change A and B to normal index numbers, such as 0 and 1. I tried the following:

myDf = myDf.reset_index()

It worked, but I see an additional column named index which has values A and B. I tried dropping this,

 myDf.drop('index', axis=1, inplace=True)

but now I see another column named level_0.

Could you point out what is the right approach to this? Thanks

$\endgroup$
3
  • $\begingroup$ myDf.reset_index(inplace=True) If you want to drop also the old index myDf.reset_index(inplace=True, drop=True) $\endgroup$ Apr 13, 2022 at 9:01
  • 1
    $\begingroup$ Thanks it worked. Would you like to add it as an answer so that I could accept it? $\endgroup$
    – Karl 17302
    Apr 16, 2022 at 7:24
  • $\begingroup$ yes, thank you. Glad it helped $\endgroup$ Apr 20, 2022 at 7:28

1 Answer 1

0
$\begingroup$

The solution would be :

myDf.reset_index(inplace=True) 

If you want to drop also the old index

myDf.reset_index(inplace=True, drop=True)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.