DataFrame constructor not properly called

20,644

Solution 1

The pd.DataFrame constructor does not accept a dictionary view as data. You can convert to list instead. 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

Equivalently, you can use pd.DataFrame.from_dict, which accepts a dictionary directly:

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

Solution 2

This worked for me

df = pd.Dataframe([data])
Share:
20,644
Mak Haj
Author by

Mak Haj

Updated on April 01, 2021

Comments

  • Mak Haj
    Mak Haj about 3 years

    I am trying to create a dataframe with Python, which raise the Error in the qustion title

      # 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
    
    • Mak Haj
      Mak Haj over 5 years
      tc_dataframe = pd.DataFrame(token_cnt_table.values(), index=token_cnt_table.keys()) the error always in this line
    • FHTMitchell
      FHTMitchell over 5 years
      What is the full error traceback?