将列表设置为 pandas 数据框的一列中的值 [英] set list as value in a column of a pandas dataframe

查看:75
本文介绍了将列表设置为 pandas 数据框的一列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据框 df ,我想创建一个填充0的新列,我使用:

Let's say I have a dataframe df and I would like to create a new column filled with 0, I use:

df['new_col'] = 0

到目前为止,没有问题.但是,如果我要使用的值是列表,那么它将不起作用:

This far, no problem. But if the value I want to use is a list, it doesn't work:

df['new_col'] = my_list

ValueError: Length of values does not match length of index

我知道为什么这行不通(pandas试图为列的每个单元格分配列表的一个值),但是如何避免这种行为呢?(如果不清楚,我希望新列的每个单元格都包含相同的预定义列表)

I understand why this doesn't work (pandas is trying to assign one value of the list per cell of the column), but how can we avoid this behavior? (if it isn't clear I would like every cell of my new column to contain the same predefined list)

注意:我也尝试过: df.assign(new_col = my_list),同样的问题

Note: I also tried: df.assign(new_col = my_list), same problem

推荐答案

您必须这样做:

df['new_col'] = [my_list] * len(df)

示例:

In [13]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
df

Out[13]:
          a         b         c
0 -0.010414  1.859791  0.184692
1 -0.818050 -0.287306 -1.390080
2 -0.054434  0.106212  1.542137
3 -0.226433  0.390355  0.437592
4 -0.204653 -2.388690  0.106218

In [17]:
df['b'] = [[234]] * len(df)
df

Out[17]:
          a      b         c
0 -0.010414  [234]  0.184692
1 -0.818050  [234] -1.390080
2 -0.054434  [234]  1.542137
3 -0.226433  [234]  0.437592
4 -0.204653  [234]  0.106218

请注意,dfs已针对标量值进行了优化,在我看来,存储非标量值使过滤,查找,获取和设置变得麻烦,以至于变得烦人

Note that dfs are optimised for scalar values, storing non scalar values defeats the point in my opinion as filtering, looking up, getting and setting become problematic to the point that it becomes a pain

这篇关于将列表设置为 pandas 数据框的一列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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