简体   繁体   中英

pandas "DataFrame constructor not properly called" when given a list of Series

I have a bunch of pd.Series stored as a Python list , and I want to create a pd.DataFrame with those Series as its rows . The following code works for the latest pandas version (1.1.3), but fails in my setup:

df = pd.DataFrame(ls_series)
pandas.core.common.PandasError: DataFrame constructor not properly called!

(Win)Python==3.6.1. pandas==0.19.2, numpy==1.11.3

Calling pd.concat with axis=1 will create a DataFrame with those Series as columns. Then just use .transpose() or .T :

df = pd.concat(ls_series, axis=1).T

One idea is convert list of Series to dict of Series and pass to DataFrame.from_dict :

ls_series = [pd.Series([1,2,6], name='a'), 
             pd.Series([10,20,60], name='b'), 
             pd.Series([11,12,61], name='c')]

df = pd.DataFrame.from_dict({x.name: x for x in ls_series}, orient='index')
print (df)
    0   1   2
a   1   2   6
b  10  20  60
c  11  12  61

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM