我有以下df1
:
prueba
12-03-2018 7
08-03-2018 1
06-03-2018 9
05-03-2018 5
我想让列中的每个值都按最后一个(5
)开始,然后用该值减去整个列。然后向上迭代并减去列中的剩余值。对于每次减法,我想生成一个列并生成一个带有每个减法结果的df:
所需的输出将是这样的:
05-03-2018 06-03-2018 08-03-2018 12-03-2018
12-03-2018 2 -2 6 0
08-03-2018 -4 -8 0 NaN
06-03-2018 4 0 NaN NaN
05-03-2018 0 NaN NaN NaN
我尝试获得所需的输出,首先是df1
和
df2=df1.sort_index(ascending=True)
创建一个空的df:
main_df=pd.DataFrame()
然后迭代列df2
中的值并减去df1
列
for index, row in df2.iterrows():
datos=df1-row['pruebas']
df=pd.DataFrame(data=datos,index=index)
if main_df.empty:
main_df= df
else:
main_df=main_df.join(df)
print(main_df)
但是以下错误输出:
TypeError: Index(...) must be called with a collection of some kind, '05-03-2018' was passed
答案 0 :(得分:2)
您可以使用np.triu
,使用数组减法
s=df.prueba.values.astype(float)
s=np.triu((s-s[:,None]).T)
s[np.tril_indices(s.shape[0], -1)]=np.nan
pd.DataFrame(s,columns=df.index,index=df.index).reindex(columns=df.index[::-1])
Out[482]:
05-03-2018 06-03-2018 08-03-2018 12-03-2018
12-03-2018 2.0 -2.0 6.0 0.0
08-03-2018 -4.0 -8.0 0.0 NaN
06-03-2018 4.0 0.0 NaN NaN
05-03-2018 0.0 NaN NaN NaN
答案 1 :(得分:1)