[英]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 .我有一堆
pd.Series
存储为 Python list
,我想创建一个pd.DataFrame
与这些系列作为其行。 The following code works for the latest pandas version (1.1.3), but fails in my setup:以下代码适用于最新的 Pandas 版本 (1.1.3),但在我的设置中失败:
df = pd.DataFrame(ls_series)
pandas.core.common.PandasError: DataFrame constructor not properly called!
(Win)Python==3.6.1. (赢)Python==3.6.1。 pandas==0.19.2, numpy==1.11.3
熊猫==0.19.2,numpy==1.11.3
Calling pd.concat
with axis=1
will create a DataFrame with those Series as columns.使用
axis=1
调用pd.concat
将创建一个 DataFrame ,将这些系列作为列。 Then just use .transpose()
or .T
:然后只需使用
.transpose()
或.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
:一种想法是将 Series 列表转换为 Series dict 并传递给
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.