matrix - Python - pandas - Append Series into Blank DataFrame -
say have 2 pandas series in python:
import pandas pd h = pd.series(['g',4,2,1,1]) g = pd.series([1,6,5,4,"abc"])
i can create dataframe h , append g it:
df = pd.dataframe([h]) df1 = df.append(g, ignore_index=true)
i get:
>>> df1 0 1 2 3 4 0 g 4 2 1 1 1 1 6 5 4 abc
but suppose have empty dataframe , try append h it:
df2 = pd.dataframe([]) df3 = df2.append(h, ignore_index=true)
this not work. think problem in second-to-last line of code. need somehow define blank dataframe have proper number of columns.
by way, reason trying scraping text internet using requests+beautifulsoup , processing , trying write dataframe 1 row @ time.
so if don't pass empty list dataframe constructor works:
in [16]: df = pd.dataframe() h = pd.series(['g',4,2,1,1]) df = df.append(h,ignore_index=true) df out[16]: 0 1 2 3 4 0 g 4 2 1 1 [1 rows x 5 columns]
the difference between 2 constructor approaches appears index dtypes
set differently, empty list int64
nothing object
:
in [21]: df = pd.dataframe() print(df.index.dtype) df = pd.dataframe([]) print(df.index.dtype) object int64
unclear me why above should affect behaviour (i'm guessing here).
update
after revisiting can confirm looks me bug in pandas version 0.12.0
original code works fine:
in [13]: import pandas pd df = pd.dataframe([]) h = pd.series(['g',4,2,1,1]) df.append(h,ignore_index=true) out[13]: 0 1 2 3 4 0 g 4 2 1 1 [1 rows x 5 columns]
i running pandas 0.13.1
, numpy 1.8.1
64-bit using python 3.3.5.0
think problem pandas upgrade both pandas , numpy safe, don't think 32 versus 64-bit python issue.
Comments
Post a Comment