1 回答
TA贡献1798条经验 获得超3个赞
我建议使用列表理解方法,使用模板数据框更新空数据框:
template = pd.DataFrame(data = [[pd.NA, pd.NA, pd.NA, pd.NA]], columns = [0,1,2,3])
dataframes= [i if not i.empty else template for i in dataframes]
这将i.empty == True使用 中定义的数据帧更新数据帧template。一个工作示例:
df1 = pd.DataFrame(data = [[102000000,2000000,1400000,0]], columns = [0,1,2,3])
df2 = pd.DataFrame()
df3 = pd.DataFrame()
df4 = pd.DataFrame()
df5 = pd.DataFrame(data = [[102000000,2000000,1400000,0]], columns = [0,1,2,3])
dataframes = [df1, df2, df3,df4,df5]
template = pd.DataFrame(data = [[pd.NA, pd.NA, pd.NA, pd.NA]], columns = [0,1,2,3])
dataframes= [i if not i.empty else template for i in dataframes]
pd.concat(dataframes)
0 1 2 3
0 102000000 2000000 1400000 0
0 <NA> <NA> <NA> <NA>
0 <NA> <NA> <NA> <NA>
0 <NA> <NA> <NA> <NA>
0 102000000 2000000 1400000 0
添加回答
举报