简体   繁体   English

如何根据另一列值填充空索引或空行?

[英]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 :因为有空字符串首先将它们转换为失踪的价值观Series.mask再往前填充缺失值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.head(4)来“取消分组”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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何填充(基于数据框的索引)空列 - How to fill (based on the index of a dataframe) an empty column 根据另一列 Pandas,如果它们不为空,则用它们的邻居填充空单元格 - Fill the empty cells with their neighbours if they are not empty based on another column Pandas 根据标识符用另一行的另一列的值填充空列 - Fill empty columns with values from another column of another row based on an identifier 如果数据值列中的空值已经存在于另一行中,则使用该值填充该值 - Fill in empty value in a dataframe column with the same value if it already exists in another row Pandas - 根据哪一列不为空在新列中填充值 - Pandas - fill value in new column based on which column is not empty 根据另一列中的值在一行中的空列中填充多个值 - Populating several values in an empty column in a row, based on a value from another column 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe 根据同一行中另一列的值填充缺失值 - Fill missing value based on value from another column in the same row 如果其他两个列在Pandas中具有匹配的值,如何用另一个数据框的值填充空列的值? - How to fill empty column values with another dataframe's value if two other columns have matching values in Pandas? 如何根据另一列填充 nan 值 - how to fill nan value based on another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM