1

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

1 Answer 1

2

Use Index.difference with Index.append for new Index and columns values with no sorted values and change position by DataFrame.reindex:

print (df_1.index.difference(df.index))
Index(['B', 'D'], dtype='object', name='name')

print (df.index.append(df_1.index.difference(df.index)))
Index(['Z', 'C', 'A', 'B', 'D'], dtype='object', name='name')

idx = df.index.append(df_1.index.difference(df.index))
cols = df.columns.append(df_1.columns.difference(df.columns))
df_1 = df_1.reindex(index=idx, columns=cols)
print (df_1)
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
Sign up to request clarification or add additional context in comments.

3 Comments

thank you for your suggestion. I have followed it well but I would like to fix rows and columns from the previous table as my expected result
@HookIm - oops, I miss changed columns, please check edited answer.
im thinking over, I'm almost to search the how to re-column LOL. thank you for your suggestion again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.