following to this post How to reorder indexed rows based on a list in Pandas data frame
import pandas as pd
df = pd.DataFrame({'name' : ['A', 'Z','C'],
'company' : ['Apple', 'Yahoo','Amazon'],
'height' : [130, 150,173]})
df = df.pivot(index="name", columns="company", values="height").fillna(0)
df.reindex(["Z", "C", "A"])
company Amazon Apple Yahoo
name
Z 0.0 0.0 150.0
C. 173.0 0.0 0.0
A 0.0 130.0 0.0
I wonder if I added more data and do this by following to this link Is there a way to copy only the structure (not the data) of a Pandas DataFrame?
df_1 = pd.DataFrame({'name' : ['A','Z','B','C','D'],
'company' : ['Apple','Yahoo','Alebaba','Amazon','Google'],
'height' : [130, 150,160,173,180]})
df_1 = df_1.pivot(index="name", columns="company", values="height").fillna(0)
df_1 = df_1.reindex_like(df)
the result is being like below
company Amazon Apple Yahoo
name
Z 0.0 0.0 150.0
C 173.0 0.0 0.0
A 0.0 130.0 0.0
but I would like to see the result like this
company Amazon Apple Yahoo Alebaba Google
name
Z 0.0 0.0 150.0 0.0 0.0
C 173.0 0.0 0.0 0.0 0.0
A 0.0 130.0 0.0 0.0 0.0
B 0.0 0.0 0.0 160.0 0.0
D 0.0 0.0 0.0 0.0 180.0
this is fine with the small data but in case of thousand data, how can I fix this issues?
the dataset that will be added to the previous data can locate in any position.
any suggestion? T T