【问题标题】:How to transform a list into a Dataframe Matrix如何将列表转换为数据框矩阵
【发布时间】:2017-06-02 10:59:26
【问题描述】:

我有一个包含两列的列表,我想将其用作矩阵的行和列索引,另一列用作数据。我怎样才能像使用 csv 文件一样构建矩阵?

这是我的清单。我想将count 作为数据,eclipse_id 作为索引,最后一个作为列索引:

In[31]: listado

Out[31]:[{'count': 1L, 'eclipse_id': 10616, 'subscriber_id': 13},
 {'count': 1L, 'eclipse_id': 10337, 'subscriber_id': 13},
 {'count': 1L, 'eclipse_id': 9562, 'subscriber_id': 13},
 {'count': 1L, 'eclipse_id': 10660, 'subscriber_id': 13},
 {'count': 1L, 'eclipse_id': 10621, 'subscriber_id': 13},

我的尝试:

pd.DataFrame(data=listado[1:,0],
            index=listado[2:,0]
            columns=listado[3:,0])

With the error message :

  File "<ipython-input-33-f87ac772eb69>", line 3
    columns=listado[3:,0])
          ^
SyntaxError: invalid syntax

输出应该是这样的:

subscriber_id  13   14    15     16
eclipse_id       
9562            1    1     0    ...
10337           1    0     0    ...
10616           1    2     0    ...
10621           1    1     1
10660           1    0     0

【问题讨论】:

  • 在你的ctor参数pd.DataFrame(data=listado[1:,0], index=listado[2:,0], #&lt;- here columns=listado[3:,0])中缺少逗号,即使你添加了逗号,你也不能通过传递一个元组来分割列表,这就是使用[1:,0]会发生的事情
  • 另外,您能否发布所需的输出,因为您的语法不正确,不清楚您真正想要实现的目标是什么

标签: python pandas matrix dataframe jupyter-notebook


【解决方案1】:

看来你需要pivot:

listado = [
{'count': 1, 'eclipse_id': 10616, 'subscriber_id': 13},
{'count': 1, 'eclipse_id': 10337, 'subscriber_id': 13},
{'count': 1, 'eclipse_id': 9562, 'subscriber_id': 13},
{'count': 1, 'eclipse_id': 10660, 'subscriber_id': 13},
{'count': 1, 'eclipse_id': 10621, 'subscriber_id': 13}]

df = pd.DataFrame(listado)
print (df)
   count  eclipse_id  subscriber_id
0      1       10616             13
1      1       10337             13
2      1        9562             13
3      1       10660             13
4      1       10621             13

df = df.pivot(index='eclipse_id', columns='subscriber_id', values='count')
print (df)
subscriber_id  13
eclipse_id       
9562            1
10337           1
10616           1
10621           1
10660           1

或者:

df = df.set_index(['eclipse_id','subscriber_id'])['count'].unstack(fill_value=0)
print (df)
subscriber_id  13
eclipse_id       
9562            1
10337           1
10616           1
10621           1
10660           1

如果重复需要meansum...的聚合数据...:

listado = [
{'count': 5, 'eclipse_id': 9562, 'subscriber_id': 13},
{'count': 4, 'eclipse_id': 9562, 'subscriber_id': 13},
{'count': 1, 'eclipse_id': 9562, 'subscriber_id': 13},
{'count': 1, 'eclipse_id': 10660, 'subscriber_id': 13},
{'count': 1, 'eclipse_id': 10621, 'subscriber_id': 13}]

df = pd.DataFrame(listado)
print (df)
   count  eclipse_id  subscriber_id
0      5        9562             13 < same 9562, 13, different 5
1      4        9562             13 < same 9562, 13, different 4
2      1        9562             13 < same 9562, 13, different 1
3      1       10660             13
4      1       10621             13

df = df.groupby(['eclipse_id','subscriber_id'])['count'].mean().unstack(fill_value=0)
print (df)
subscriber_id        13
eclipse_id             
9562           3.333333 <- (5+4+1)/3 = 3.333
10621          1.000000
10660          1.000000

pivot_table:

df = df.pivot_table(index='eclipse_id', 
                    columns='subscriber_id', 
                    values='count', 
                    aggfunc='mean')
print (df)
subscriber_id        13
eclipse_id             
9562           3.333333 <- (5+4+1)/3 = 3.333
10621          1.000000
10660          1.000000

【讨论】:

  • 非常感谢您的帮助 jezrael !但是我不知道为什么,但是当我执行 df = pd.DataFrame(listado) 时,我的 df 输出看起来不像您的输出,就像在 following screenshot 中显示的那样。错误是:KeyError: 'subscriber_id'
  • 它给了我:[nan, 6413.0, 6521.0, 6525.0, 6527.0, 6528.0, 6530.0...。我在计算 df = df.pivot(index='subscriber_id', columns='eclipse_id', values='count') 时没有问题,但在打印 df 时出现上述问题。
  • 问题是,你需要什么?因为选择subscriber_id 是不可能的,因为它只是columns.name。通过print (df.columns.name)查看它
  • 接下来如果使用df = pd.DataFrame(listado) 然后print (df.dtypes) 那么它返回eclipse_idfloat。问题是缺少某些值,因此获取NaNs 并将所有值转换为floats。解决方案是首先将NaNs 替换为0 之类的标量,然后转换为int-df['eclipse_id'] = df['eclipse_id'].fillna(0).astype(int)。然后使用pivot。 ;)
  • 检查commnet之前,如果使用df['eclipse_id'] = df['eclipse_id'].fillna(0).astype(int)得到它;)
猜你喜欢
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多