Python - pandas - add a series to an empty data area

Let's say I have two pandas series in python:

import pandas as pd h = pd.Series(['g',4,2,1,1]) g = pd.Series([1,6,5,4,"abc"]) 

I can only create a DataFrame with h and then add g to 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 now suppose I have an empty DataFrame, and I'm trying to add h to it:

 df2 = pd.DataFrame([]) df3 = df2.append(h, ignore_index=True) 

This does not work. I think the problem is in the second line of code. I need to somehow define an empty DataFrame for the correct number of columns.

By the way, the reason I'm trying to do this is because I clear the text from the Internet using + BeautifulSoup requests, and I process it and try to write it to the DataFrame one line at a time.
+6
source share
1 answer

So, if you do not pass an empty list to the DataFrame constructor, it 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 the two approaches to the constructors is that the dtypes index is specified differently, and the empty list is Int64 , and nothing is object :

 In [21]: df = pd.DataFrame() print(df.index.dtype) df = pd.DataFrame([]) print(df.index.dtype) object int64 

It is not clear why the above should affect the behavior (I assume here).

UPDATE

After revising this question, I can confirm that this looks like an error in pandas version 0.12.0 , since your source code is working fine:

 In [13]: import pandas as 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 run pandas 0.13.1 and numpy 1.8.1 64-bit using python 3.3.5.0 , but I think the problem is in pandas, but I would update both pandas and numpy to be safe, I don’t think it is 32 -bit problem with 64-bit python.

+8
source

Source: https://habr.com/ru/post/970132/


All Articles