def make_data_frame(words, years, feature_dict):
"""
Makes a pandas dataframe for word, years, and dictionary of feature funcs.
Each feature func should take (word, year) and return feature value.
Constructed dataframe has flat csv style structure and missing values are removed.
"""
temp = collections.defaultdict(list)
feature_dict["word"] = lambda word, year : word
feature_dict["year"] = lambda word, year : year
for word in words:
for year in years:
for feature, feature_func in feature_dict.iteritems():
temp[feature].append(feature_func(word, year))
df = pd.DataFrame(temp)
df = df.replace([np.inf, -np.inf], np.nan)
df = df.dropna()
return df
评论列表
文章目录