【问题标题】:Changing dictionary consisting 16k dicts to a Pandas Dataframe将包含 16k 字典的字典更改为 Pandas 数据框
【发布时间】:2015-07-22 12:08:29
【问题描述】:

我正在为我的硕士论文研究数据挖掘问题。我正在使用 Python 进行数据分析,但我没有使用 Pandas 的经验,这是将我的数据转换为 Dataframe 所必需的。为了使用名为 Lifelines 的 Python 包进行生存回归,我需要从我的实验数据字典中创建一个协变量矩阵,其中包含超过 16k 的字典以及关于 Kickstarter 项目的 Twitter 数据(请参阅下面的示例字典)。

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}

如果我从这个字典创建一个 Pandas 数据框,我将能够使用 Patsy 创建一个协变量矩阵,例如:

X = patsy.dmatrix('friends + followers + retweets, favorites -1', data, return_type='dataframe') 

现在我的问题是如何从experiment_data 字典创建熊猫数据框?内部字典的键(目标、启动、追随者等)应该是每个 Kickstarter 项目的列(即索引编号:0 到 16041)。

任何帮助将不胜感激。提前致谢!

附:如果您有使用 Python 和 Lifelines 进行生存回归的经验,请告诉我!

【问题讨论】:

    标签: python pandas dataframe survival-analysis patsy


    【解决方案1】:

    我想你想要from_dict 使用参数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 
    

    【讨论】:

      猜你喜欢
      • 2019-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 2015-10-20
      • 2019-02-22
      • 2017-07-16
      • 2020-12-22
      • 2017-10-04
      相关资源
      最近更新 更多