Adding values for missing data combinations in Pandas

前端 未结 2 2022
情深已故
情深已故 2020-12-18 04:44

I\'ve got a pandas data frame containing something like the following:

person_id   status    year    count
0           \'pass\'    1980    4
0           \'fa         


        
2条回答
  •  情歌与酒
    2020-12-18 04:56

    create a MultiIndex by MultiIndex.from_product() and then set_index(), reindex(), reset_index().

    import pandas as pd
    import io
    
    all_person_ids = [0, 1, 2]
    all_statuses = ['pass', 'fail']
    all_years = [1980, 1981, 1982]
    df = pd.read_csv(io.BytesIO("""person_id   status    year    count
    0           pass    1980    4
    0           fail    1982    1
    1           pass    1981    2"""), delim_whitespace=True)
    names = ["person_id", "status", "year"]
    
    mind = pd.MultiIndex.from_product(
        [all_person_ids, all_statuses, all_years], names=names)
    df.set_index(names).reindex(mind, fill_value=0).reset_index()
    

提交回复
热议问题