1
にそれらを置く:私は必要なものパンダ - 私はそうのように国によって、日付ごとにマルチインデックスされるデータフレーム持って列の値のシリーズをフェッチし、細胞
をテーブルを作成することですそれは、国ごとにグループ化され、それはそうのような日付ごとの測定値の配列と余分な列があります。シーケンスは、いくつかの国のために存在するが、ミスしている日付のゼロが含まれていることを
注意を他の人のためにing。
私はこれを行うことができる唯一の方法は、それぞれの国を反復処理することです:
# discard dates and create dataframe grouped by countries
grouped_chunk = grouped_tds_df.groupby('country__name').sum()
# create index containing uninterrupted sequence of dates
full_date_range = pd.date_range(datetime(2017, 4, 13).date(), datetime(2017, 4, 18).date())
# iterate over each country
for country_idx in grouped_chunk.index:
# get rows that contain data for this country
this_country_raws = grouped_tds_df[grouped_tds_df['country__name'] == country_idx]
# reindex them to include missing dates
this_country_raws = this_country_raws.set_index('date').reindex(
full_date_range, fill_value=0
)
# pick a list of values for sequence
joined = ','.join(str(l) for l in this_country_raws['raws'])
# insert sequence into original table
grouped_chunk.loc[country_idx, 'raws_sequence'] = joined
しかし、反復なしでそれを行う方法はありますか?元のテーブルのインデックスレベル2(date
)の一部のバッチ再インデックス化?私は各国の再インデックスを開始したくありません。
これは素晴らしいです、ありがとう! – kurtgn
@kurtgn回答をアップアップすることも自由にしてください – piRSquared