Is It Possible From Dataframe Transform To Matrix?
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
Solution 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)
Post a Comment for "Is It Possible From Dataframe Transform To Matrix?"