向数据框的所有行添加值

编程入门 行业动态 更新时间:2024-10-25 18:33:35
本文介绍了向数据框的所有行添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有两个 Pandas 数据帧 df1(长度为 2)和 df2(长度约为 30 行).df1 的索引值总是不同的,并且永远不会出现在 df2 中.我想将 df1 列的平均值添加到 df2 的相应列.示例:将 0.6 添加到 c1 的所有行,将 0.9 添加到 c2 的所有行等...

I have two pandas dataframes df1 (of length 2) and df2 (of length about 30 rows). Index values of df1 are always different and never occur in df2. I would like to add the average of columns from df1 to corresponding columns of df2. Example: add 0.6 to all rows of c1 and 0.9 to all rows of c2 etc ...

df1: 
  Date       c1   c2   c3   c4    c5   c6 ...  c10
2017-09-10  0.5  0.6  1.2   0.7  1.3  1.8 ...  1.3
2017-09-11  0.7  1.2  1.3   0.4  0.7  0.4 ...  1.5


df2:
  Date       c1   c2   c3   c4    c5   c6 ...  c10
2017-09-12  0.9  0.1  1.4   0.9  1.5  1.9 ...  1.9
2017-09-13  0.2  1.8  1.2   1.4  2.7  0.8 ...  1.1
    :                                  :  
    :                                  :     
2017-10-10  1.5  0.9  1.5   0.9  1.6  1.8 ...  1.7
2017-10-11  2.7  1.1  1.9   0.4  0.8  0.8 ...  1.3

我该怎么做?

推荐答案

当在 df1 上使用 mean 时,它默认计算每一列并产生一个 pd.系列.

When using mean on df1, it calculates over each column by default and produces a pd.Series.

pd.Series 添加到 pd.DataFrame 时,它会将 pd.Series 的索引与pd.DataFrame 并默认沿 pd.DataFrame... 的索引广播.

When adding adding a pd.Series to a pd.DataFrame it aligns the index of the pd.Series with the columns of the pd.DataFrame and broadcasts along the index of the pd.DataFrame... by default.

唯一棘手的一点是处理 Date 列.

The only tricky bit is handling the Date column.

选项 1

m = df1.mean()
df2.loc[:, m.index] += m

df2

         Date   c1   c2    c3    c4   c5   c6  c10
0  2017-09-12  1.5  1.0  2.65  1.45  2.5  3.0  3.3
1  2017-09-13  0.8  2.7  2.45  1.95  3.7  1.9  2.5
2  2017-10-10  2.1  1.8  2.75  1.45  2.6  2.9  3.1
3  2017-10-11  3.3  2.0  3.15  0.95  1.8  1.9  2.7

如果我知道 'Date' 总是在第一列,我可以:

If I know that 'Date' is always in the first column, I can:

df2.iloc[:, 1:] += df1.mean()
df2

         Date   c1   c2    c3    c4   c5   c6  c10
0  2017-09-12  1.5  1.0  2.65  1.45  2.5  3.0  3.3
1  2017-09-13  0.8  2.7  2.45  1.95  3.7  1.9  2.5
2  2017-10-10  2.1  1.8  2.75  1.45  2.6  2.9  3.1
3  2017-10-11  3.3  2.0  3.15  0.95  1.8  1.9  2.7

<小时>

选项 2
请注意,我在 set_index 中使用了 append=True 参数,以防万一索引中有您不想弄乱的内容.


Option 2
Notice that I use the append=True parameter in the set_index just incase there are things in the index you don't want to mess up.

df2.set_index('Date', append=True).add(df1.mean()).reset_index('Date')

         Date   c1   c2    c3    c4   c5   c6  c10
0  2017-09-12  1.5  1.0  2.65  1.45  2.5  3.0  3.3
1  2017-09-13  0.8  2.7  2.45  1.95  3.7  1.9  2.5
2  2017-10-10  2.1  1.8  2.75  1.45  2.6  2.9  3.1
3  2017-10-11  3.3  2.0  3.15  0.95  1.8  1.9  2.7

如果你不关心索引,你可以把它缩短为

If you don't care about the index, you can shorten this to

df2.set_index('Date').add(df1.mean()).reset_index()

         Date   c1   c2    c3    c4   c5   c6  c10
0  2017-09-12  1.5  1.0  2.65  1.45  2.5  3.0  3.3
1  2017-09-13  0.8  2.7  2.45  1.95  3.7  1.9  2.5
2  2017-10-10  2.1  1.8  2.75  1.45  2.6  2.9  3.1
3  2017-10-11  3.3  2.0  3.15  0.95  1.8  1.9  2.7

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

更多推荐

[db:关键词]

本文发布于:2023-04-23 08:16:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!