简体   繁体   English

将常量 numpy 数组值分配给 Pandas 数据框列

[英]Assign constant numpy array value to pandas dataframe column

I would like to assign constant numpy array value to pandas dataframe column.我想将常量 numpy 数组值分配给 pandas 数据框列。

Here is what I tried:这是我尝试过的:

import pandas as pd
import numpy as np

my_df = pd.DataFrame({'col_1': [1,2,3], 'col_2': [4,5,6]})
my_df['new'] = np.array([]) # did not work
my_df['new'] = np.array([])*len(df) # did not work

Here is what worked:这是有效的:

my_df['new'] = my_df['new'].apply(lambda x: np.array([]))

I am curious why it works with simple scalar, but does not work with numpy array.我很好奇为什么它适用于简单的标量,但不适用于 numpy 数组。 Is there simpler way to assign numpy array value?有没有更简单的方法来分配 numpy 数组值?

Your "new" column will contains arrays, so it must be a object type column.您的“新”列将包含数组,因此它必须是对象类型列。

The simplest way to initialize it is :初始化它的最简单方法是:

my_df = pd.DataFrame({'col_1': [1,2,3], 'col_2': [4,5,6]})
my_df['new']=None

You can then fill it as you want.然后,您可以根据需要填充它。 For example :例如:

for index,(a,b,_)  in my_df.iterrows():
    my_df.loc[index,'new']=np.arange(a,b)
#     
#    col_1  col_2        new
# 0      1      4  [1, 2, 3]
# 1      2      5  [2, 3, 4]
# 2      3      6  [3, 4, 5]    

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

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