pandas将dataframe列单元格初始化为空列表

时间:2016-05-24 13:29:18

标签: python python-3.x pandas dataframe series

我需要在import cgi import cgitb; cgitb.enable() form = cgi.FieldStorage() serial_no = form.getvalue('serial') description = form.getvalue('description') DataFrame的列中初始化单元格。

lists

我想知道在时间效率方面有更好的方法吗?

2 个答案:

答案 0 :(得分:3)

尝试apply

df1['some_col'] = ''
df1['some_col'] = df1['some_col'].apply(list)

样品:

df1 = pd.DataFrame({'a': pd.Series([1,2])})
print (df1)
   a
0  1
1  2

df1['some_col'] = ''
df1['some_col'] = df1['some_col'].apply(list)
print (df1)
   a some_col
0  1       []
1  2       []

答案 1 :(得分:2)

因为您正在寻找时间效率,低于一些基准。我认为list理解已经非常快,可以创建空listlist个对象,但您可以使用itertools.repeat来缩小边际改进。在insert篇文章中,apply慢了3倍,因为它循环:

import pandas as pd
from itertools import repeat
df = pd.DataFrame({"A":np.arange(100000)})

%timeit df['some_col'] = [[] for _ in range(len(df))]
100 loops, best of 3: 8.75 ms per loop

%timeit df['some_col'] = [[] for i in repeat(None, len(df))]
100 loops, best of 3: 8.02 ms per loop

%%timeit 
df['some_col'] = ''
df['some_col'] = df['some_col'].apply(list)
10 loops, best of 3: 25 ms per loop