相关疑难解决方法(0)

将字典列表转换为pandas DataFrame

我有一个这样的词典列表:

[{'points': 50, 'time': '5:00', 'year': 2010}, 
{'points': 25, 'time': '6:00', 'month': "february"}, 
{'points':90, 'time': '9:00', 'month': 'january'}, 
{'points_h1':20, 'month': 'june'}]
Run Code Online (Sandbox Code Playgroud)

我想把它变成DataFrame像这样的熊猫:

      month  points  points_h1  time  year
0       NaN      50        NaN  5:00  2010
1  february      25        NaN  6:00   NaN
2   january      90        NaN  9:00   NaN
3      june     NaN         20   NaN   NaN
Run Code Online (Sandbox Code Playgroud)

注意:列的顺序无关紧要.

如何将字典列表转换为pandas DataFrame,如上所示?

python dictionary dataframe pandas

550
推荐指数
4
解决办法
25万
查看次数

将pandas DataFrame转换为嵌套的dict

我正在寻找一种将DataFrame转换为嵌套字典的通用方法

这是一个示例数据框

    name    v1  v2  v3
0   A       A1  A11 1
1   A       A2  A12 2
2   B       B1  B12 3
3   C       C1  C11 4
4   B       B2  B21 5
5   A       A2  A21 6
Run Code Online (Sandbox Code Playgroud)

列数可能不同,列名也可能不同.

像这样 :

{
'A' : { 
    'A1' : { 'A11' : 1 }
    'A2' : { 'A12' : 2 , 'A21' : 6 }} , 
'B' : { 
    'B1' : { 'B12' : 3 } } , 
'C' : { 
    'C1' : { …
Run Code Online (Sandbox Code Playgroud)

python pandas

21
推荐指数
2
解决办法
1万
查看次数

关于dicts的词典到DataFrame的词典

我想将JSON数据存储在Python Pandas DataFrame中

我的JSON数据是像这样的dicts的词典

d = {
  "col1": {
    "row1": {
      "data1": "0.87", 
      "data2": "Title col1", 
      "data3": "14.4878", 
      "data4": "Title row1"
    }, 
    "row2": {
      "data1": "15352.3", 
      "data2": "Title col1", 
      "data3": "14.9561", 
      "data4": "Title row2"
    }, 
    "row3": {
      "data1": "0", 
      "data2": "Title col1", 
      "data3": "16.8293", 
      "data4": "Title row3"
    }
  }, 
  "col2": {
    "row1": {
      "data1": "0.87", 
      "data2": "Title col2", 
      "data3": "24.4878", 
      "data4": "Title row1"
    }, 
    "row2": {
      "data1": "15352.3", 
      "data2": "Title col2", 
      "data3": "24.9561", 
      "data4": "Title row2"
    }, 
    "row3": {
      "data1": …
Run Code Online (Sandbox Code Playgroud)

python json dictionary pandas

14
推荐指数
1
解决办法
5856
查看次数


将命名元组嵌套字典到 pandas 数据框

我的命名元组定义如下:

In[37]: from collections import namedtuple
        Point = namedtuple('Point', 'x y')
Run Code Online (Sandbox Code Playgroud)

嵌套字典具有以下格式:

In[38]: d
Out[38]: 
{1: {None: {1: Point(x=1.0, y=5.0), 2: Point(x=4.0, y=8.0)}},
2: {None: {1: Point(x=45324.0, y=24338.0), 2: Point(x=45.0, y=38.0)}}}
Run Code Online (Sandbox Code Playgroud)

我正在尝试从字典 d 创建一个 pandas 数据框,而不必执行 for 循环。

我通过执行以下操作成功地从字典的子集创建了数据框:

In[40]: df=pd.DataFrame(d[1][None].values())

In[41]: df

Out[41]: 
   x  y
0  1  5
1  4  8
Run Code Online (Sandbox Code Playgroud)

但我希望能够从整个字典创建数据框。

我希望数据帧输出以下内容(我使用多索引表示法):

In[42]: df
Out[42]:
Subcase Step ID  x       y
1       None 1   1.0     5.0
             2   4.0     8.0
2       None 1   45324.0 24338.0
             2   45.0    38.0
Run Code Online (Sandbox Code Playgroud)

DataFrame的from_dict方法最多只支持两层嵌套,所以我无法使用它。我也在考虑修改d字典的结构来实现我的目标。此外,也许它不一定是一本字典。

谢谢。

python dictionary namedtuple dataframe pandas

5
推荐指数
1
解决办法
1318
查看次数

如何将嵌套字典转换为pandas数据框?

我有以下格式的字典“ my_dict”:

{'l1':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}},
 'l2':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}}
}
Run Code Online (Sandbox Code Playgroud)

目前,我正在使用pd.DataFrame.from_dict(my_dict, orient='index')并获得像这样的df:

                             c2                           c1
l1  {u'a': 3, u'c': 5, u'b': 4}  {u'a': 0, u'c': 2, u'b': 1}
l2  {u'a': 3, u'c': 5, u'b': 4}  {u'a': 0, u'c': 2, u'b': 1}
Run Code Online (Sandbox Code Playgroud)

但是,我想要的是l1 / l2和c2 / c3作为索引,而a / b / c作为列。
像这样:

       a   b   c
l1 c1  0   1 …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

5
推荐指数
1
解决办法
2989
查看次数

将我的字典变成熊猫数据框

我有一个函数,它根据某些条件创建多个字典。

但是,我真的很想在收集后将 dict 转换为数据框。但我找不到一个简单的方法来做到这一点......现在我认为解决方案是将字典中的每个键乘以最内部字典中的键数,但希望有更好的方法

由于我的函数创建了 dict,如果有更好的方法,我可以以任何方式更改它。

这是我现在的字典

{'TSLA': {2011: {'negative': {'lowPrice': 185.16,
    'lowDate': '05/27/19',
    'highPrice': 365.71,
    'highDate': '12/10/18',
    'change': -0.49}},
  2012: {'negative': {'lowPrice': 185.16,
    'lowDate': '05/27/19',
    'highPrice': 365.71,
    'highDate': '12/10/18',
    'change': -0.49}},
  2013: {'negative': {'lowPrice': 32.91,
    'lowDate': '01/07/13',
    'highPrice': 37.24,
    'highDate': '03/26/12',
    'change': -0.12},
   'positive': {'lowPrice': 32.91,
    'lowDate': '01/07/13',
    'highPrice': 190.9,
    'highDate': '09/23/13',
    'change': 4.8}}}}
Run Code Online (Sandbox Code Playgroud)

我想要的输出是这样的,当然还有值:

                    lowPrice lowDate highPrice highDate change
ATVI  2012 Negative      NaN     NaN       NaN      NaN  NaN
           Positive      NaN     NaN       NaN      NaN  NaN
      2013 Negative      NaN     NaN       NaN …
Run Code Online (Sandbox Code Playgroud)

python dictionary pandas

4
推荐指数
2
解决办法
210
查看次数

如何将这种嵌套的JSON以列式形式转换为Pandas数据帧

我可以将这种嵌套的JSON格式以列式格式读入pandas.

JSON方案

JSON方案格式

在此输入图像描述

Python脚本

    req = requests.get(REQUEST_API)
    returned_data = json.loads(req.text)
    # status
    print("status: {0}".format(returned_data["status"]))
    # api version
    print("version: {0}".format(returned_data["version"]))
    data_in_columnar_form = pd.DataFrame(returned_data["data"])
    data = data_in_columnar_form["data"]
Run Code Online (Sandbox Code Playgroud)

UPDATE

我想将以下JSON方案转换为表格格式,如何表格?

排队

JSON方案

     "data":[  
        {  
           "year":"2009",
           "values":[  
              {  
                 "Actual":"(0.2)"
              },
              {  
                 "Upper End of Range":"-"
              },
              {  
                 "Upper End of Central Tendency":"-"
              },
              {  
                 "Lower End of Central Tendency":"-"
              },
              {  
                 "Lower End of Range":"-"
              }
           ]
        },
        {  
           "year":"2010",
           "values":[  
              {  
                 "Actual":"2.8"
              },
              {  
                 "Upper End of Range":"-"
              },
              {  
                 "Upper End of Central …
Run Code Online (Sandbox Code Playgroud)

python pandas

3
推荐指数
1
解决办法
6241
查看次数

如何使用列表从嵌套字典构建MultiIndex Pandas DataFrame

我有以下字典.

d= {'key1': {'sub-key1': ['a','b','c','d','e']},
    'key2': {'sub-key2': ['1','2','3','5','8','9','10']}}
Run Code Online (Sandbox Code Playgroud)

这篇文章的帮助下,我成功地将这个字典转换为DataFrame.

df = pd.DataFrame.from_dict({(i,j): d[i][j] 
                            for i in d.keys() 
                            for j in d[i].keys()},
                            orient='index')
Run Code Online (Sandbox Code Playgroud)

但是,我的DataFrame采用以下形式:

                  0  1  2  3  4     5     6
(key1, sub-key1)  a  b  c  d  e  None  None
(key2, sub-key2)  1  2  3  5  8     9    10
Run Code Online (Sandbox Code Playgroud)

我可以使用元组作为索引值,但我认为使用多级DataFrame更好.像这样的帖子帮助我分两步创建它,但是我很难一步完成(即从最初的创建),因为字典中的列表以及之后的元组添加了一个级别并发症.

python dictionary multi-index dataframe pandas

2
推荐指数
1
解决办法
2099
查看次数

标签 统计

pandas ×9

python ×9

dictionary ×6

dataframe ×4

json ×1

multi-index ×1

namedtuple ×1