1

I have a data frame:

             Date                Cities       Random_Number
Country
US           2020-01-01          LA           100
             2020-01-03          LA           150
UK           2020-01-01          Ldn          125
             2020-01-03          Birmingham   135

My desired data frame:

             Date                Cities       Random_Number
Country
US           2020-01-01          LA           100
US           2020-01-03          LA           150
UK           2020-01-01          Ldn          125
UK           2020-01-03          Birmingham   135

My aim is to have empty index row to be filled. Many thanks.

5
  • 1
    do df.reset_index() Commented Mar 5, 2020 at 12:33
  • did that, but didnt work. Commented Mar 5, 2020 at 12:34
  • Waht is print (df.index) in first DataFrame ? Commented Mar 5, 2020 at 12:34
  • Index(['US','','UK','']),dtype = 'object',name = 'Country'.. Commented Mar 5, 2020 at 12:36
  • Try - df.reset_index().set_index('Country') Commented Mar 5, 2020 at 12:38

3 Answers 3

1

Because there are empty strings first convert them to missing values by Series.mask and then forward filling missing values by ffill:

df = df.reset_index()
print (df)
  Country        Date      Cities  Random_Number
0      US  2020-01-01          LA            100
1          2020-01-03          LA            150
2      UK  2020-01-01         Ldn            125
3          2020-01-03  Birmingham            135

df['Country'] = df['Country'].mask(df['Country'] == '').ffill()
print (df)
  Country        Date      Cities  Random_Number
0      US  2020-01-01          LA            100
1      US  2020-01-03          LA            150
2      UK  2020-01-01         Ldn            125
3      UK  2020-01-03  Birmingham            135
Sign up to request clarification or add additional context in comments.

Comments

0

can you try this

data.fillna(method='ffill')

Got your desired output.

Comments

0

You can try df.head(4) to 'ungroup' the DataFrame.

df = pd.DataFrame([['US', '2020-01-01', 'LA', 100],
                   ['US', '2020-01-03', 'LA', 150],
                   ['UK', '2020-01-01', 'Ldn', 125],
                   ['UK', '2020-01-03', 'Birmingham', 135]],
                  columns=['Country', 'Date', 'Cities', 'Random_Number']).groupby('Country')
print(df)

Result:

             Date                Cities       Random_Number
Country
US           2020-01-01          LA           100
             2020-01-03          LA           150
UK           2020-01-01          Ldn          125
             2020-01-03          Birmingham   135  

Ungroup:

print(df.head(4))

Result:

  Country        Date      Cities  Random_Number
0      US  2020-01-01          LA            100
1      US  2020-01-03          LA            150
2      UK  2020-01-01         Ldn            125
3      UK  2020-01-03  Birmingham            135

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.