我正在为硕士论文研究数据挖掘问题。我使用Python进行数据分析,但我没有使用Pandas的经验,这是将数据转换为Dataframe所必需的。为了使用名为Lifelines的Python包进行生存回归,我需要从我的experiment_data dict创建一个Covariate Matrix,其中包含超过16k的关于Kickstarter项目的Twitter数据(参见下面的示例dict)。
16041: {'goal': 1200, 'launch': 1353544772, 'days-before-deadline': 3, 'followers': 149, 'date-funded': 1355887690.9189188, 'id': 52687, 'tweet_ids': [280965208409796608, ... n], 'state': 1, 'deadline': 1356136772, 'retweets': 0, 'favorites': 0, 'duration': 31, 'timestamps': [1355876412.0], 'favourites': 0, 'runtime': 27, 'friends': 127, 'pledges': [0.0, 0.0625, 0.0625, ... n], 'statuses': 7460}
如果我从这个dict创建一个Pandas Dataframe,我将能够使用Patsy创建一个Covariate Matrix,例如:
X = patsy.dmatrix('friends + followers + retweets, favorites -1', data, return_type='dataframe')
现在我的问题是如何从experiment_data dicts创建一个Pandas Dataframe?内部词典(目标,发射,追随者等)的键应该是每个Kickstarter项目的列(即索引号:0到16041)。
任何帮助都会非常感激。提前谢谢!
P.S。如果您有使用Python和Lifelines的生存回归经验,请告诉我们!
答案 0 :(得分:1)
我认为你希望from_dict
使用param orient='index'
:
In [31]:
d={16041: {'goal': 1200, 'launch': 1353544772, 'days-before-deadline': 3, 'followers': 149, 'date-funded': 1355887690.9189188, 'id': 52687, 'tweet_ids': [280965208409796608], 'state': 1, 'deadline': 1356136772, 'retweets': 0, 'favorites': 0, 'duration': 31, 'timestamps': [1355876412.0], 'favourites': 0, 'runtime': 27, 'friends': 127, 'pledges': [0.0, 0.0625, 0.0625], 'statuses': 7460}}
pd.DataFrame.from_dict(d, orient='index')
Out[31]:
id followers days-before-deadline statuses duration state \
16041 52687 149 3 7460 31 1
goal tweet_ids pledges favourites \
16041 1200 [280965208409796608] [0.0, 0.0625, 0.0625] 0
deadline favorites retweets runtime friends launch \
16041 1356136772 0 0 27 127 1353544772
timestamps date-funded
16041 [1355876412.0] 1.355888e+09