简体   繁体   English

未正确调用 DataFrame 构造函数

[英]DataFrame constructor not properly called

I am trying to create a dataframe with Python, which raise the Error in the qustion title我正在尝试使用 Python 创建一个数据框,这会在问题标题中引发错误

  # pre processing to get G-Test score
    def G_test(tokens, types):
        tokens_cnt = tokens.value_counts().astype(float)
        types_cnt = types.value_counts().astype(float)
        total_cnt = float(sum(tokens_cnt))
    
        # calculate each token counts
        token_cnt_table = collections.defaultdict(lambda : collections.Counter())
        for _tokens, _types in zip(tokens.values, types.values):
            token_cnt_table[_tokens][_types] += 1
    

 tc_dataframe = pd.DataFrame(token_cnt_table.values(), index=token_cnt_table.keys())

        tc_dataframe.fillna(0, inplace=True)
        for column in tc_dataframe.columns.tolist():
            tc_dataframe[column+'_exp'] = (tokens_cnt / total_cnt) * types_cnt[column]
            c_dataframe[column+'_GTest'] = [G_test_score(tkn_count, exp) for tkn_count, exp in zip(tc_dataframe[column], tc_dataframe[column+'_exp'])]
            return tc_dataframe

The pd.DataFrame constructor does not accept a dictionary view as data. pd.DataFrame构造函数不接受字典视图作为数据。 You can convert to list instead.您可以改为转换为list Here's a minimal example:这是一个最小的例子:

d = {'a': 1, 'b': 2, 'c': 3}

df = pd.DataFrame(d.values(), index=d.keys())
# PandasError: DataFrame constructor not properly called!

df = pd.DataFrame(list(d.values()), index=d.keys())
# Works!

The docs do suggest this:文档确实建议这样做:

data : numpy ndarray (structured or homogeneous), dict, or DataFrame数据numpy ndarray(结构化或同类)、dict 或 DataFrame

Equivalently, you can use pd.DataFrame.from_dict , which accepts a dictionary directly:等效地,您可以使用pd.DataFrame.from_dict ,它直接接受字典:

df = pd.DataFrame.from_dict(d, orient='index')

这对我有用

df = pd.Dataframe([data])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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