【问题标题】:Creating a DataFrame from a dictionary of arrays从数组字典创建 DataFrame
【发布时间】:2020-01-18 03:54:40
【问题描述】:

我对 Python 还是很陌生,在 Pyhton 中将字典转换为 DataFrame 时遇到了困难。 我的字典包含不同股票在不同日期上涨的概率。当我尝试将其转换为 DataFrame 时,股票名称被视为列名,这正是我想要的方式。我的问题是,所有值都出现在每列的第一行。

这基本上是我尝试使用的代码:

一开始我有一个这样的股票样本:

stocks = ['MSFT', 'AAPL', 'AMZN']

为了获得向上运动的概率,我使用了以下代码:

proba = {stock: clf[stock].predict_proba(X_test[stock]) for stock in stocks}

print(proba)

给我以下输出:

{'MSFT': array([[0.30994211],
   [0.15608782],
   [0.15608782],
   [0.16334815],
   [0.14721092],
   [0.29563944],
   [0.16334815],
   [0.24821587],
   [0.43182074],
   [0.30994211],
   [0.28825953],
   .
   .
   .
   [0.34160564]]), 'AAPL': array([[0.48241034],
   [0.47819121],
   [0.48937013],
   [0.49798732],
   [0.50132104],
   .
   .
   . 
   [0.03298367]]), 'AMZN': array([[0.51179782],
   [0.64532595],
   [0.56331474],
   [0.66499856],
   [0.55492011],
   [0.4623048 ],
   [0.4536123 ],
   [0.4613901 ],
   [0.39305493],
   [0.44297254],
   .
   .
   .])}

我现在的目标是将此字典转换为如下所示的 DataFrame:

    MSFT    AAPL    AMZN
0   0.875   0.983   0.276
1   0.345   0.765   0.342
2   0.654   0.444   0.874  
    ...     ...     ...
    ...     ...     ...

最后,DataFrame 应该有 280 行和 3 列。

这里有一个小样本可供使用:

proba = {stock: clf[stock].predict_proba(X_test[stock]) for stock in stocks}
proba = {stock: np.delete(proba[stock], 0, axis=1) for stock in stocks}
print(proba)

结果是:

{'MSFT': array([[0.49784439],
   [0.51812552],
   [0.35948374]]), 'AAPL': array([[0.29038393],
   [0.58038393],
   [0.52032512]]), 'AMZN': array([[0.64295894],
   [0.54295894],
   [0.39719920]])}

这些数组应该被转换成一个如下所示的 DataFrame:

     MSFT         AAPL         AMZN
0    0.49784439   0.29038393   0.64295894
1    0.51812552   0.58038393   0.54295894
2    0.35948374   0.52032512   0.39719920

希望编辑使它更清晰一些。

【问题讨论】:

  • 您能否仅提供示例输入和输出。您添加的代码 sn-ps 未按预期运行。也许是this 格式的东西。

标签: python arrays dataframe dictionary


【解决方案1】:

您不应该将 dict 放在列表中,只需使用 pandas.DataFrame(proba)。 我会推荐使用DataFrame.from_dict 函数,它会使用默认参数给出相同的结果:

In [1]: import pandas
In [2]: d = {'a' : [1,2,3], 'b':[4,5,6], 'c':[7,8,9]}
In [3]: pandas.DataFrame.from_dict(d)
Out[3]:
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

但也可以让你改变字典的方向,我觉得这很有用。 如果您的 dict 键是您的索引列,您可以将 orient 传递给函数。您可能还想使用列来命名列:

In [4]: pandas.DataFrame.from_dict(d, orient='index', columns=['first','second','hird'])
Out[4]:
   first  second  hird
a      1       2     3
b      4       5     6
c      7       8     9

【讨论】:

  • 感谢您的回答。我也尝试使用 pandas.DataFrame(proba),但随后出现错误 ValueError: If using all scalar values, you must pass an index
【解决方案2】:
# Convert list of lists into list
for key in res.keys():
     res[key] = [x for sublist in res[key] for x in sublist]

# Read dictionary into DataFrame
df = pd.DataFrame.from_dict(res)

您必须先将列表列表转换为单个列表,然后再将其转换为 DataFrame。

【讨论】:

  • 我已经尝试过使用DataFrame.from_records() 不幸的是,您的解决方案引发了与上述相同的错误:ValueError: If using all scalar values, you must pass an index
  • @TomRoider 我看到您自上次以来已经显着改变了您的问题。提供我们可以使用的简短示例数据。
  • 我提供了一个简短的示例,希望对您有所帮助
猜你喜欢
  • 1970-01-01
  • 2021-09-14
  • 2019-02-10
  • 2015-10-29
  • 2021-12-07
  • 2021-03-08
  • 1970-01-01
  • 2020-07-06
  • 1970-01-01
相关资源
最近更新 更多