简体   繁体   中英

How to fill empty index or empty row based on another column value?

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.

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

can you try this

data.fillna(method='ffill')

Got your desired output.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM