将空列表列添加到DataFrame [英] Add column of empty lists to DataFrame

查看:72
本文介绍了将空列表列添加到DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于此问题如何向其中添加空列一个数据框?,我想知道向DataFrame添加一列空列表的最佳方法.

Similar to this question How to add an empty column to a dataframe?, I am interested in knowing the best way to add a column of empty lists to a DataFrame.

我要做的基本上是初始化一列,并在行上进行迭代以处理其中的一些行,然后在此新列中添加填充列表以替换初始化的值.

What I am trying to do is basically initialize a column and as I iterate over the rows to process some of them, then add a filled list in this new column to replace the initialized value.

例如,如果下面是我的初始DataFrame:

For example, if below is my initial DataFrame:

df = pd.DataFrame(d = {'a': [1,2,3], 'b': [5,6,7]}) # Sample DataFrame

>>> df
   a  b
0  1  5
1  2  6
2  3  7

然后我想最终得到这样的结果,其中每一行都经过单独处理(显示了示例结果):

Then I want to ultimately end up with something like this, where each row has been processed separately (sample results shown):

>>> df
   a  b          c
0  1  5     [5, 6]
1  2  6     [9, 0]
2  3  7  [1, 2, 3]

当然,如果我尝试像df['e'] = []那样使用任何其他常量进行初始化,它会认为我正在尝试添加长度为0的项目序列,因此失败.

Of course, if I try to initialize like df['e'] = [] as I would with any other constant, it thinks I am trying to add a sequence of items with length 0, and hence fails.

如果我尝试将新列初始化为NoneNaN,则在尝试将列表分配给位置时遇到以下问题.

If I try initializing a new column as None or NaN, I run in to the following issues when trying to assign a list to a location.

df['d'] = None

>>> df
   a  b     d
0  1  5  None
1  2  6  None
2  3  7  None

问题1(如果我可以采用这种方法,那将是完美的!也许我没想到一些琐碎的事情):

Issue 1 (it would be perfect if I can get this approach to work! Maybe something trivial I am missing):

>>> df.loc[0,'d'] = [1,3]

...
ValueError: Must have equal len keys and value when setting with an iterable

问题2(该方法有效,但并非没有警告,因为不能保证它可以按预期工作):

Issue 2 (this one works, but not without a warning because it is not guaranteed to work as intended):

>>> df['d'][0] = [1,3]

C:\Python27\Scripts\ipython:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

因此,我诉诸于用空列表初始化并根据需要扩展它们.我可以想到几种方法来进行这种初始化,但是还有更简单的方法吗?

Hence I resort to initializing with empty lists and extending them as needed. There are a couple of methods I can think of to initialize this way, but is there a more straightforward way?

方法1:

df['empty_lists1'] = [list() for x in range(len(df.index))]

>>> df
   a  b   empty_lists1
0  1  5             []
1  2  6             []
2  3  7             []

方法2:

 df['empty_lists2'] = df.apply(lambda x: [], axis=1)

>>> df
   a  b   empty_lists1   empty_lists2
0  1  5             []             []
1  2  6             []             []
2  3  7             []             []

问题摘要:

在问题1中是否可以解决任何小的语法更改,从而可以将列表分配给None/NaN初始化字段?

Is there any minor syntax change that can be addressed in Issue 1 that can allow a list to be assigned to a None/NaN initialized field?

如果没有,那么用空列表初始化新列的最佳方法是什么?

If not, then what is the best way to initialize a new column with empty lists?

推荐答案

另一种方法是使用

One more way is to use np.empty:

df['empty_list'] = np.empty((len(df), 0)).tolist()


当您尝试找到dflen时,也可​​以在方法1"中关闭.index.


You could also knock off .index in your "Method 1" when trying to find len of df.

df['empty_list'] = [[] for _ in range(len(df))]


结果是np.empty更快...


Turns out, np.empty is faster...

In [1]: import pandas as pd

In [2]: df = pd.DataFrame(pd.np.random.rand(1000000, 5))

In [3]: timeit df['empty1'] = pd.np.empty((len(df), 0)).tolist()
10 loops, best of 3: 127 ms per loop

In [4]: timeit df['empty2'] = [[] for _ in range(len(df))]
10 loops, best of 3: 193 ms per loop

In [5]: timeit df['empty3'] = df.apply(lambda x: [], axis=1)
1 loops, best of 3: 5.89 s per loop

这篇关于将空列表列添加到DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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