我需要在import cgi
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
serial_no = form.getvalue('serial')
description = form.getvalue('description')
到DataFrame
的列中初始化单元格。
lists
我想知道在时间效率方面有更好的方法吗?
答案 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
理解已经非常快,可以创建空list
个list
个对象,但您可以使用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