【问题标题】:converting sparse dataframe to dense dataframe将稀疏数据帧转换为密集数据帧
【发布时间】:2016-09-09 10:56:43
【问题描述】:

我在数据框中存储了稀疏数据:

df = pd.DataFrame({'a':[1,3,5], 'b':[2,5,5], 'data':np.random.randn(3)})

a   b   data
0   1   2   -0.824022
1   3   5   0.503239
2   5   5   -0.540105

因为我关心的是空数据,所以实际数据看起来像这样:

true_df

    a   b   data
0   1   1   NaN
1   1   2   -0.824022
2   1   3   NaN
3   1   4   NaN
4   1   5   NaN
5   2   1   NaN
6   2   2   NaN
7   2   3   NaN
8   2   4   NaN
9   2   5   NaN
10  3   1   NaN
11  3   2   NaN
12  3   3   NaN
13  3   4   NaN
14  3   5   0.503239
15  4   1   NaN
16  4   2   NaN
17  4   3   NaN
18  4   4   NaN
19  4   5   NaN
20  5   1   NaN
21  5   2   NaN
22  5   3   NaN
23  5   4   NaN
24  5   5   -0.540105

我的问题是如何构造true_df?我希望有某种方法可以使用pd.concat 或pd.merge,即构造一个数据框,它是密集表的形状,然后连接两个数据框,但没有以预期的方式连接(列不合并)。最终目标是以 a 和 b 为中心。

作为跟进,因为我认为 kinjo 是正确的,为什么这仅适用于整数而不适用于浮点数?使用:

import pandas as pd
import numpy as np

df = pd.DataFrame({'a':[1.0,1.3,1.5], 'b':[1.2,1.5,1.5], 'data':np.random.randn(3)})

### Create all possible combinations of a,b
newindex = [(b,a) for b in np.arange(1,df.b.max()+0.1, 0.1) for a in np.arange(1,df.a.max()+0.1,0.1)]

### Set the index as a,b and reindex
df.set_index(['a','b']).reindex(newindex).reset_index()

将返回:

    a   b   data
0   1.0 1.0 NaN
1   1.0 1.1 NaN
2   1.0 1.2 NaN
3   1.0 1.3 NaN
4   1.0 1.4 NaN
5   1.0 1.5 NaN
6   1.0 1.6 NaN
7   1.1 1.0 NaN
8   1.1 1.1 NaN
9   1.1 1.2 NaN
10  1.1 1.3 NaN
11  1.1 1.4 NaN
12  1.1 1.5 NaN
13  1.1 1.6 NaN
14  1.2 1.0 NaN
15  1.2 1.1 NaN
16  1.2 1.2 NaN
17  1.2 1.3 NaN
18  1.2 1.4 NaN
19  1.2 1.5 NaN
20  1.2 1.6 NaN
21  1.3 1.0 NaN
22  1.3 1.1 NaN
23  1.3 1.2 NaN
24  1.3 1.3 NaN
25  1.3 1.4 NaN
26  1.3 1.5 NaN
27  1.3 1.6 NaN
28  1.4 1.0 NaN
29  1.4 1.1 NaN
30  1.4 1.2 NaN
31  1.4 1.3 NaN
32  1.4 1.4 NaN
33  1.4 1.5 NaN
34  1.4 1.6 NaN
35  1.5 1.0 NaN
36  1.5 1.1 NaN
37  1.5 1.2 NaN
38  1.5 1.3 NaN
39  1.5 1.4 NaN
40  1.5 1.5 NaN
41  1.5 1.6 NaN
42  1.6 1.0 NaN
43  1.6 1.1 NaN
44  1.6 1.2 NaN
45  1.6 1.3 NaN
46  1.6 1.4 NaN
47  1.6 1.5 NaN
48  1.6 1.6 NaN

【问题讨论】:

  • 我不明白输出 5 2 3 -0.540105 中的行。这样对吗? DataFrame 的构造函数在系列中有 a 5 并且输出是 3...
  • 我的例子是错误的,我改变了它以消除不准确。

标签: python pandas merge


【解决方案1】:

重新索引是一个简单的解决方案。类似于@jezrael 的解决方案,但不需要合并。

import pandas as pd
import numpy as np

df = pd.DataFrame({'a':[1,3,5], 'b':[2,5,5], 'data':np.random.randn(3)})

### Create all possible combinations of a,b
newindex = [(b,a) for b in range(1,df.b.max()+1) for a in range(1,df.a.max()+1)]

### Set the index as a,b and reindex
df.set_index(['a','b']).reindex(newindex)

如果您想将数字计数作为您的总索引,则可以重置索引。

如果你的索引是浮动的you should use linspace and not arange:

import pandas as pd
import numpy as np

df = pd.DataFrame({'a':[1.0,1.3,1.5], 'b':[1.2,1.5,1.5], 'data':np.random.randn(3)})

### Create all possible combinations of a,b
newindex = [(b,a) for b in np.linspace(a_min, a_max, a_step, endpoint=False) for a in np.linspace(b_min, b_max, b_step, endpoint=False)]

### Set the index as a,b and reindex
df.set_index(['a','b']).reindex(newindex).reset_index()

【讨论】:

  • 查看我上面的编辑:跟进问题,如果我们将 a 和 b 更改为浮点数,则会失败。为什么?例如:[1] df = pd.DataFrame({'a':[1.0,1.3,1.5], 'b':[1.2,1.5,1.5], 'data':np.random.randn(3)})[2] newindex = [(b,a) for b in np.arange(1,df.b.max()+0.1, 0.1) for a in np.arange(1,df.a.max()+0.1,0.1)]
  • @mnky9800n 不确定为什么重新索引在浮点数上失败。以下堆栈问题有所讨论:stackoverflow.com/questions/17351233/…
  • 我假设它与凌乱的浮点错误有关。 numpy.arange documentation 提到了这一点,这与您发布的堆栈问题相吻合。
  • 四舍五入 a,b 为我解决了这个问题。浮点精度导致了这个问题。 (round(a,1), round(b,1)) ...
【解决方案2】:

由于您打算旋转 a 和 b,因此您可以通过

获得旋转后的结果
import numpy as np
import pandas as pd
df = pd.DataFrame({'a':[1,3,5], 'b':[2,5,5], 'data':np.random.randn(3)})

result = pd.DataFrame(np.nan, index=range(1,6), columns=range(1,6))
result.update(df.pivot(index='a', columns='b', values='data'))
print(result)

产生

    1         2   3   4         5
1 NaN  0.436389 NaN NaN       NaN
2 NaN       NaN NaN NaN       NaN
3 NaN       NaN NaN NaN -1.066621
4 NaN       NaN NaN NaN       NaN
5 NaN       NaN NaN NaN  0.328880

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2016-02-01
    • 2016-09-19
    • 2013-12-25
    • 1970-01-01
    • 2015-09-14
    相关资源
    最近更新 更多