将一系列数据分配给dataFrame的所有列(按列)? [英] Assign a series to ALL columns of the dataFrame (columnwise)?

查看:88
本文介绍了将一系列数据分配给dataFrame的所有列(按列)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,并且其垂直尺寸与df相同,我想分配 该系列到DataFrame的所有列. 为什么这样做是自然的?

I have a dataframe, and series of the same vertical size as df, I want to assign that series to ALL columns of the DataFrame. What is the natural why to do it ?

例如

df = pd.DataFrame([[1, 2 ], [3, 4], [5 , 6]] ) 
ser = pd.Series([1, 2, 3 ])

我希望"df"的所有列都等于"ser".

I want all columns of "df" to be equal to "ser".

PS相关:

  • 一种通过答案解决问题的方法:

  • One way to solve it via answer: How to assign dataframe[ boolean Mask] = Series - make it row-wise ? I.e. where Mask = true take values from the same row of the Series (creating all true mask), but I guess there should be some more simple way.

如果我不需要全部,但需要一些列,请在此处给出答案: 将系列分配给熊猫数据框的几行

If I need NOT all, but SOME columns - the answer is given here: Assign a Series to several Rows of a Pandas DataFrame

推荐答案

使用 to_frame 但是从

But it seems easier is solution from comment, there was added columns parameter if need same order columns as original with real data:

df = pd.DataFrame({c:ser for c in df.columns}, columns=df.columns)

这篇关于将一系列数据分配给dataFrame的所有列(按列)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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