Is it possible from dataframe transform to Matrix?

后端 未结 1 1571
谎友^
谎友^ 2021-01-27 14:05

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             


        
相关标签:
1条回答
  • 2021-01-27 14:48

    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)
    
    0 讨论(0)
提交回复
热议问题