假设我想用循环创建并填充一个空数据框架的值。
假设您想使用循环创建并填充空数据帧的值。
追加操作会产生对角矩阵,即使忽略索引(ignore index)为false:
假设您想使用循环创建并填充空数据帧的值。
import pandas as pd
import numpy as np
years = [2013, 2014, 2015]
dn=pd.DataFrame()
for year in years:
df1 = pd.DataFrame({'Incidents': [ 'C', 'B','A'],
year: [1, 1, 1 ],
}).set_index('Incidents')
print (df1)
dn=dn.append(df1, ignore_index = False)
追加操作会产生对角矩阵,即使忽略索引(ignore index)为false:
>>> dn
2013 2014 2015
Incidents
C 1 NaN NaN
B 1 NaN NaN
A 1 NaN NaN
C NaN 1 NaN
B NaN 1 NaN
A NaN 1 NaN
C NaN NaN 1
B NaN NaN 1
A NaN NaN 1
[9 rows x 3 columns]
它应该像这样:
>>> dn
2013 2014 2015
Incidents
C 1 1 1
B 1 1 1
A 1 1 1
[3 rows x 3 columns]
有没有更好的方法?并且有没有修复附加的方法?
我的pandas版本是“0.13.1-557-g300610e”