Reputation: 37
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
Upvotes: 2
Views: 42462
Reputation: 164613
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')
Upvotes: 11