Is it possible from dataframe transform to Matrix?

纵饮孤独 提交于 2019-12-25 00:15:32

问题


I am newbie in python, I have a huge dataframe:

Person  OD
A       BS1
A       BS2
B       BS4
B       BS8
C       BS5
C       BS1
D       BS9
D       BS7
E       BS2
E       BS7
F       BS2
F       BS1
G       BS1
G       BS2

is it possible to transform into an origin-destination (OD) matrix in python-pandas? Example from BS1 to BS2 there is 2 person (A and G) then in OD matrix 2 people into BS1-BS2.

my expected result:

O/D BS1 BS2 BS3 BS4 BS5 BS6 BS7 BS8 BS9
BS1     2                           
BS2 1                       1       
BS3                                 
BS4                             1   
BS5 1                               
BS6                                 
BS7                                 
BS8                                 
BS9                         1   

how to do it? thanks a lot


回答1:


Following is a solution.

places = df["OD"].unique()
places.sort()
od_df = pd.DataFrame(df["OD"].values.reshape((-1, 2)), columns=["O", "D"])
od_matrix = od_df.groupby(["O", "D"]).size().unstack().reindex(index=places, columns=places)
od_matrix.fillna(0, downcast="infer", inplace=True)

You can also use pd.pivot_table and replace the fourth line with

od_matrix = pd.pivot_table(od_df, index="O", columns="D", aggfunc="size").reindex(index=places, columns=places)


来源:https://stackoverflow.com/questions/56520616/is-it-possible-from-dataframe-transform-to-matrix

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