映射多个数据框和填充列的值

时间:2018-01-17 09:44:22

标签: python pandas dataframe

假设我有以下三个数据帧:

数据框1:

df1 = {'year': ['2010','2012','2014','2015'], 'count': [1,1,1,1]}
df1 = pd.DataFrame(data=df1)
df1 = df1.set_index('year')
df1

year    count
2010    1
2012    1
2014    1
2015    1

数据框2:

df2 = {'year': ['2010','2011','2016','2017'], 'count': [2,1,3,1]}
df2 = pd.DataFrame(data=df2)
df2 = df2.set_index('year')
df2

year    count
2010    2
2011    1
2016    3
2017    1

数据框3:

df3 = {'year': ['2010','2011','2012','2013','2014','2015','2017'], 'count': [4,2,5,4,4,1,1]}
df3 = pd.DataFrame(data=df3)
df3 = df3.set_index('year')
df3

year    count
2010    4
2011    2
2012    5
2013    4
2014    4
2015    1
2017    1

现在我希望有三个数据帧,包含所有年份和数量。例如,如果df1缺少年份 2011,2013,2016,2017 ,那么这些将添加到df1的索引中,每个新添加的索引的计数为0。

所以我的输出对于df1来说是这样的:

year    count
2010    1
2012    1
2014    1
2015    1
2011    0
2013    0
2016    0
2017    0

同样对于df2和df3也是如此。感谢。

4 个答案:

答案 0 :(得分:3)

您可以union使用reindex

from functools import reduce
idx = reduce(np.union1d,[df1.index, df2.index, df3.index])
print (idx)

['2010' '2011' '2012' '2013' '2014' '2015' '2016' '2017']

另一种解决方案:

df1 = df1.reindex(idx, fill_value=0)
print (df1)
      count
year       
2010      1
2011      0
2012      1
2013      0
2014      1
2015      1
2016      0
2017      0
df2 = df2.reindex(idx, fill_value=0)
print (df2)
      count
year       
2010      2
2011      1
2012      0
2013      0
2014      0
2015      0
2016      3
2017      1
df3 = df3.reindex(idx, fill_value=0)
print (df3)
      count
year       
2010      4
2011      2
2012      5
2013      4
2014      4
2015      1
2016      0
2017      1
absolute

答案 1 :(得分:3)

reindex上使用all_years

In [257]: all_years = df1.index | df2.index | df3.index

In [258]: df1.reindex(all_years, fill_value=0)
Out[258]:
      count
year
2010      1
2011      0
2012      1
2013      0
2014      1
2015      1
2016      0
2017      0

In [259]: df2.reindex(all_years, fill_value=0)
Out[259]:
      count
year
2010      2
2011      1
2012      0
2013      0
2014      0
2015      0
2016      3
2017      1

答案 2 :(得分:1)

我会选择联盟,你也可以使用唯一的,即

idx = pd.Series(np.concatenate([df1.index,df2.index,df3.index])).unique()
# or idx = set(np.concatenate([df1.index,df2.index,df3.index])) 
df1.reindex(idx).fillna(0)

      count
year       
2010    1.0
2012    1.0
2014    1.0
2015    1.0
2011    0.0
2016    0.0
2017    0.0
2013    0.0

答案 3 :(得分:0)

也可以使用迭代:

# find missing years:
morelist = [ j            # items which satisfy following criteria
             # list of all numbers converted to strings:
             for j in map(lambda x: str(x), range(2010, 2018, 1))
             if  j not in df1.index  ]      # those not in current index

# create a dataframe to be added:
df2add = pd.DataFrame(data=[0]*len(morelist),   
                      columns=['count'], 
                      index=morelist)

# add new dataframe to original:
df1 = pd.concat([df1, df2add]) 

print(df1)

输出:

      count
2010      1
2012      1
2014      1
2015      1
2011      0
2013      0
2016      0
2017      0