[英]How to prevent pandas from only assigning value from one df to column of another for only one row?
I have a df that looks like this:我有一个 df 看起来像这样:
id col1 col2
1 2 3
4 5 6
7 8 9
when I go to add a new column and assign a value like this:当我去添加一个新列并分配一个这样的值时:
df['new_col'] = old_df['email']
The assignment only assigns the value to the first like so:赋值仅将值分配给第一个,如下所示:
id col1 col2 new_col
1 2 3 a@a.com
4 5 6 NaN
7 8 9 NaN
How do I have the assignment for all rows like so:我如何为所有行分配像这样的:
id col1 col2 new_col
1 2 3 a@a.com
4 5 6 a@a.com
7 8 9 a@a.com
edit:编辑:
old_df: old_df:
id col3 col4 email
1 2 3 a@a.com
Pandas series assignment works by index . Pandas 系列分配按索引工作。 Since old_df
only contains index 0
, only index 0
, ie the first row, of df
is updated.由于old_df
仅包含索引0
,因此仅更新索引0
,即df
的第一行。
For your particular problem, you can use iat
and assign a scalar to a series:对于您的特定问题,您可以使用iat
并为一系列分配标量:
df['new_col'] = old_df['email'].iat[0]
This works because Pandas broadcasts scalars to the whole series irrespective of index.这是有效的,因为 Pandas 将标量广播到整个系列而不考虑索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.