python - Pandas 创建完美的面板数据,根据条件附加空行

标签 python pandas panel-data

我正在寻找一种在满足两个条件的情况下将空行附加到数据帧的方法。条件是,如果在特定年份中找不到索引 ID,则代码将添加一个具有索引“ID”和年份但其他列为空的空行。最终目的是创建一个完美的面板数据集,其中每个观察值都代表 7 次(基于年份),尽管可能存在来自某些观察值的数据,例如1次或3次(这不是恒定的,而是不时变化的)。否则,除了索引“ID”和年份之外,这些缺失的数据行将为空。

这是我的数据框 all_data 目前的示例:

ID      Year      Data1      Data2
345     2010        3          2
345     2011        1          4
345     2012        5          2
345     2013        3          1
345     2014        3          1
345     2015        3          1
345     2016        3          1
123     2010        1          1
123     2012        0          2
123     2016        0          2

这是我正在寻找的示例。

ID      Year      Data1      Data2
345     2010        3          2
345     2011        1          4
345     2012        5          2
345     2013        3          1
345     2014        3          1
345     2015        3          1
345     2016        3          1
123     2010        1          1
123     2011                  
123     2012        0          2
123     2013
123     2014
123     2015
123     2016        0          2

我有超过 200 个观察值和 20 个数据列,因此手动执行此操作需要太多时间。这是我尝试过的,但它不起作用。它返回相同的数据帧并且不添加任何空行。 “missing”是一个列表,其中包含可以从 all_data 数据帧中找到的每个唯一 ID。

missing = ['345', '123']
sub_dfs = []
for year in [ 2010, 2011, 2012, 2013, 2014, 2015, 2016 ]:
    sub_df = all_data.loc[ all_data[ 'Year' ] == year ].copy()
    if( year == 2010):
        sub_df.set_index( 'ID', inplace=True)
        sub_df.reindex(sub_df.index.union(missing))
    if (year == 2011):
        sub_df.set_index('ID', inplace=True)
        sub_df.reindex(sub_df.index.union(missing))
    if (year == 2012):
        sub_df.set_index('ID', inplace=True)
        sub_df.reindex(sub_df.index.union(missing))
    if (year == 2013):
        sub_df.set_index('ID', inplace=True)
        sub_df.reindex(sub_df.index.union(missing))
    if (year == 2014):
        sub_df.set_index('ID', inplace=True)
        sub_df.reindex(sub_df.index.union(missing))
    if (year == 2015):
        sub_df.set_index('ID', inplace=True)
        sub_df.reindex(sub_df.index.union(missing))
    if (year == 2016):
        sub_df.set_index('ID', inplace=True)
        sub_df.reindex(sub_df.index.union(missing))
    sub_dfs.append(sub_df)

new_data = pd.concat(sub_dfs)

预先感谢您的帮助!

最佳答案

使用reindexMultiIndex.from_product 创建的 Multiindex全部unique IDnp.arange 的值按最小和最大 years:

mux = pd.MultiIndex.from_product([df['ID'].unique(), 
                                  np.arange(df['Year'].min(), df['Year'].max() + 1)],
                                  names=['ID','Year'])

df =  df.set_index(['ID','Year']).reindex(mux).reset_index()
print (df)
     ID  Year  Data1  Data2
0   345  2010    3.0    2.0
1   345  2011    1.0    4.0
2   345  2012    5.0    2.0
3   345  2013    3.0    1.0
4   345  2014    3.0    1.0
5   345  2015    3.0    1.0
6   345  2016    3.0    1.0
7   123  2010    1.0    1.0
8   123  2011    NaN    NaN
9   123  2012    0.0    2.0
10  123  2013    NaN    NaN
11  123  2014    NaN    NaN
12  123  2015    NaN    NaN
13  123  2016    0.0    2.0

关于python - Pandas 创建完美的面板数据,根据条件附加空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51323311/

相关文章:

r - 如何在 R 中的面板向量自回归(pVAR)后进行格兰杰因果关系检验?

r - 创建一个矩阵,其中包含每个组周期的随机观测值

python - 本地使用 Z3Py

python - PANDAS从txt文件中读取不存在的额外列

python - pandas dataframe上的for-if循环语句操作问题

python - 无法优化此代码,想知道为什么它运行得也这么慢 - 我该如何优化此代码?

python - 用 0 向左填充一维列向量

python - 使用 python beautifulsoup 解析整个网站

python - 如何根据 df1 中的 bool 值对 df2 中的字段进行排名并创建第三个数据框?

r - 如何按个人采样/划分面板数据(最好使用插入符号库)?