向pandas DataFrame添加带有列的行 [英] Add a row with means of columns to pandas DataFrame

查看:107
本文介绍了向pandas DataFrame添加带有列的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫DataFrame,其中包含一些随时间变化的传感器读数,如下所示:

I have a pandas DataFrame consisting of some sensor readings taken over time like this:

       diode1  diode2  diode3  diode4
Time
0.530       7       0      10      16
1.218      17       7      14      19
1.895      13       8      16      17
2.570       8       2      16      17
3.240      14       8      17      19
3.910      13       6      17      18
4.594      13       5      16      19
5.265       9       0      12      16
5.948      12       3      16      17
6.632      10       2      15      17

我已经编写了代码,以每列的方式添加另一行:

I have written code to add another row with the means of each column:

# List of the averages for the test. 
averages = [df[key].describe()['mean'] for key in df]
indexes = df.index.tolist()
indexes.append('mean')
df.reindex(indexes)
# Adding the mean row to the bottom of the DataFrame

i = 0
for key in df:
    df.set_value('mean', key, averages[i])
    i += 1

这给了我想要的结果,它是一个像这样的DataFrame:

This gives me the result I want, which is a DataFrame like this:

       diode1  diode2  diode3  diode4
Time
0.53      7.0     0.0    10.0    16.0
1.218    17.0     7.0    14.0    19.0
1.895    13.0     8.0    16.0    17.0
2.57      8.0     2.0    16.0    17.0
3.24     14.0     8.0    17.0    19.0
3.91     13.0     6.0    17.0    18.0
4.594    13.0     5.0    16.0    19.0
5.265     9.0     0.0    12.0    16.0
5.948    12.0     3.0    16.0    17.0
6.632    10.0     2.0    15.0    17.0
mean     11.6     4.1    14.9    17.5

但是,我确信这不是添加行的最有效方法.我尝试将append与另存为pandas Series的方法一起使用,但最终结果如下:

However, I am sure that this is not the most efficient way of adding the row. I have tried using append with the means saved as a pandas Series but ended up with something like this:

    diode1  diode2  diode3  diode4                     mean
0      7.0     0.0    10.0    14.0                      NaN
1      9.0     0.0    10.0    15.0                      NaN
2     10.0     5.0    14.0    20.0                      NaN
3      6.0     0.0     7.0    14.0                      NaN
4      7.0     0.0    10.0    15.0                      NaN
5      7.0     0.0     8.0    14.0                      NaN
6      7.0     0.0    11.0    14.0                      NaN
7      7.0     0.0     2.0    11.0                      NaN
8      2.0     0.0     4.0    12.0                      NaN
9      4.0     0.0     0.0     6.0                      NaN
10     NaN     NaN     NaN     NaN  [11.6, 4.1, 14.9, 17.5]

我想知道是否有一种更有效的方法将带有索引均值"的行和每一列的平均值添加到熊猫DataFrame的底部.

I was wondering if there was a more efficient means of adding a row with the index 'mean' and the averages of each column to the bottom of a pandas DataFrame.

推荐答案

使用 loc for 放大设置:

df.loc['mean'] = df.mean()

结果输出:

       diode1  diode2  diode3  diode4
Time                                 
0.53      7.0     0.0    10.0    16.0
1.218    17.0     7.0    14.0    19.0
1.895    13.0     8.0    16.0    17.0
2.57      8.0     2.0    16.0    17.0
3.24     14.0     8.0    17.0    19.0
3.91     13.0     6.0    17.0    18.0
4.594    13.0     5.0    16.0    19.0
5.265     9.0     0.0    12.0    16.0
5.948    12.0     3.0    16.0    17.0
6.632    10.0     2.0    15.0    17.0
mean     11.6     4.1    14.9    17.5

这篇关于向pandas DataFrame添加带有列的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆