简体   繁体   English

给定系列列表时,熊猫“未正确调用 DataFrame 构造函数”

[英]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.

相关问题 ValueError:DataFrame 构造函数未正确调用! 当将列表中的字典覆盖到 pandas dataframe - ValueError: DataFrame constructor not properly called! when coverting dictionaries within list to pandas dataframe 硒熊猫数据框构造函数未正确调用 - selenium pandas dataframe constructor not properly called ValueError:未正确调用 DataFrame 构造函数! 与熊猫 - ValueError: DataFrame constructor not properly called! with pandas 将多个字典附加到Pandas数据框:错误的数据框构造函数未正确调用? - Appending multiple dictionaries to Pandas dataframe: Error DataFrame constructor not properly called? 未正确调用 DataFrame 构造函数 - DataFrame constructor not properly called 将列表列表转换为数据框时出现“ ValueError:DataFrame构造函数未正确调用!” - “ValueError: DataFrame constructor not properly called!” while converting list of lists to dataframe rpy2和pandas:PandasError:DataFrame构造函数未正确调用 - rpy2 and pandas: PandasError: DataFrame constructor not properly called Python Pandas滚动意味着未正确调用DataFrame构造函数 - Python Pandas rolling mean DataFrame Constructor not properly called pandas.core.common.PandasError:未正确调用DataFrame构造函数 - pandas.core.common.PandasError: DataFrame constructor not properly called DataFrame 构造函数未正确调用错误 - DataFrame error of constructor not properly called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM