据我所知,大熊猫的设计是为了装载人口稠密的大熊猫。DataFrame但我需要创建一个空的DataFrame,然后逐个添加行..做这件事最好的方法是什么?我成功地创建了一个空的DataFrame:res = DataFrame(columns=('lib', 'qty1', 'qty2'))然后,我可以添加一个新行并用以下内容填充一个字段:res = res.set_value(len(res), 'qty1', 10.0)它可以工作,但看起来很奇怪:-/(它无法添加字符串值)如何将新行添加到DataFrame(具有不同列类型)?在熊猫数据框架中添加一行
3 回答

紫衣仙女
TA贡献1839条经验 获得超15个赞
>>> import pandas as pd
>>> from numpy.random import randint
>>> df = pd.DataFrame(columns=['lib', 'qty1', 'qty2'])
>>> for i in range(5):
>>> df.loc[i] = ['name' + str(i)] + list(randint(10, size=2))
>>> df
lib qty1 qty2
0 name0 3 3
1 name1 2 4
2 name2 2 8
3 name3 2 1
4 name4 9 6

炎炎设计
TA贡献1808条经验 获得超4个赞
import pandas as pdimport numpy as np# we know we're gonna have 5 rows of datanumberOfRows = 5# create dataframedf = pd.DataFrame(index=np.arange(0, numberOfRows), columns=('lib', 'qty1', 'qty2') )# now fill it up row by rowfor x in np.arange(0, numberOfRows): #loc or iloc both work here since the index is natural numbers df.loc[x] = [np.random.randint(-1,1) for n in range(3)]In[23]: dfOut[23]: lib qty1 qty20 -1 -1 -11 0 0 02 -1 0 -13 0 -1 04 -1 0 0
速度比较
In[30]: %timeit tryThis() # function wrapper for this answerIn[31]: %timeit tryOther() # function wrapper without index (see, for example, @fred)1000 loops, best of 3: 1.23 ms per loop100 loops, best of 3: 2.31 ms per loop
增加数组(12)的大小和行数(500)使速度差异更显着:313 ms vs 2.29s
添加回答
举报
0/150
提交
取消