简体   繁体   English

熊猫-合并两个未堆叠的数据帧

[英]Pandas - combine two unstacked data frames

I'm working on a data set where I unstack the data, and get the first and last rows. 我正在处理一个数据集,在该数据集中我将数据拆栈,并获得第一行和最后一行。

>>> print df.iloc[[(0), (-1)]]

     Distance               
Node            0     1     2   
Time                            
0             27.0  54.0  97.0  
60            22.0  49.0  92.0  

then I use df1 = (df.loc[0] - df.iloc[(-1)]).unstack() to get the difference of the two rows 然后我使用df1 = (df.loc[0] - df.iloc[(-1)]).unstack()得到两行的差

Node            0    1    2    
Distance       5.0  5.0  5.0

And finally, I want to concatenate/combine the two data sets ( df and df1 ). 最后,我想串联/合并两个数据集( dfdf1 )。

 >>> print pd.concat([df, df1], axis=1)

               (Distance, 0)  (Distance, 1)  (Distance, 2) 
0                   27.0           54.0           NaN  
60                  22.0           49.0           92.0 
Distance            NaN             NaN            NaN

but I'm getting NaN . 但我得到了NaN

Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

I want my output to look like this 我希望我的输出看起来像这样

     Distance               
Node            0     1     2   
Time                            
0             27.0  54.0  97.0  
60            22.0  49.0  92.0 
Difference     5.0  5.0   5.0

In my opinion, the use of unstack is not needed for what you are trying to achieve. 我认为,您要实现的目标不需要使用unstack Below are a couple of alternatives. 以下是一些替代方案。 To simplify the formatting I've omitted index and column names. 为了简化格式,我省略了索引和列名。

Setup 设定

df = pd.DataFrame({0: [27.0, 22.0], 1: [54.0, 49.0], 2: [97.0, 92.0]},
                  index=[0, 60])

print(df)

       0     1     2
0   27.0  54.0  97.0
60  22.0  49.0  92.0

pd.DataFrame.loc pd.DataFrame.loc

Add a new row by index via pd.DataFrame.loc with a row label. 通过带有行标签的pd.DataFrame.loc通过索引添加新行。

df.loc['Distance'] = df.loc[0] - df.iloc[-1]

pd.Series.to_frame + concat pd.Series.to_frame + concat

Convert your difference series to a dataframe, then transpose and concatenate. 将差异序列转换为数据框,然后转置并连接。

diff = (df.loc[0] - df.iloc[-1]).to_frame().T
diff.index = ['Distance']

res = pd.concat([df, diff])

Result 结果

The result for both methods: 两种方法的结果:

print(res)

             0     1     2
0         27.0  54.0  97.0
60        22.0  49.0  92.0
Distance   5.0   5.0   5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM