Create Pandas DataFrame from (row, column, value) data

橙三吉。 提交于 2019-12-24 07:24:31

问题


I have a Pandas Dataframe with three columns: row, column, value. The row values are all integers below some N, and the column values are all integers below some M. The values are all positive integers.

How do I efficiently create a Dataframe with N rows and M columns, with at index i, j the value val if (i, j , val) is a row in my original Dataframe, and some default value (0) otherwise? Furthermore, is it possible to create a sparse Dataframe immediately, since the data is already quite large, but N*M is still about 10 times the size of my data?


回答1:


A NumPy solution would suit here for performance -

a = df.values
m,n = a[:,:2].max(0)+1
out = np.zeros((m,n),dtype=a.dtype)
out[a[:,0], a[:,1]] = a[:,2]
df_out = pd.DataFrame(out)

Sample run -

In [58]: df
Out[58]: 
    row  col  val
0     7    1   30
1     3    3    0
2     4    8   30
3     5    8   18
4     1    3    6
5     1    6   48
6     0    2    6
7     4    7    6
8     5    0   48
9     8    1   48
10    3    2   12
11    6    8   18

In [59]: df_out
Out[59]: 
    0   1   2  3  4  5   6  7   8
0   0   0   6  0  0  0   0  0   0
1   0   0   0  6  0  0  48  0   0
2   0   0   0  0  0  0   0  0   0
3   0   0  12  0  0  0   0  0   0
4   0   0   0  0  0  0   0  6  30
5  48   0   0  0  0  0   0  0  18
6   0   0   0  0  0  0   0  0  18
7   0  30   0  0  0  0   0  0   0
8   0  48   0  0  0  0   0  0   0


来源:https://stackoverflow.com/questions/44161912/create-pandas-dataframe-from-row-column-value-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!