1

This is the continuation of my previous post on denormalizing a DataFrame of company names.

The revised table I'm now working with is the following:

import numpy as np
import pandas as pd

df = pd.DataFrame({'name' : ['Nitron', 'Pulset', 'Rotaxi'], 
                   'postal_code' : [1410, 1020, 1310], 
                   'previous_name1' : ['Rotory', np.NaN, 'Datec'],
                   'previous_name2' : [ np.NaN, 'Cmotor', np.NaN],
                   'previous_name3' : ['Datec', np.NaN, np.NaN],
                   'country' : ['BEL', 'ENG', 'JPN'], 
                   'city' : ['Brussels', np.NaN, np.NaN]
                  })

print(df)

| name   | postal_code | previous_name1 | previous_name2 | previous_name3 | country | city     |
|--------|-------------|----------------|----------------|----------------|---------|----------|
| Nitron | 1410        | Rotory         | NaN            | Datec          | BEL     | Brussels |
| Pulset | 1020        | NaN            | Cmotor         | NaN            | ENG     | NaN      |
| Rotaxi | 1310        | Cyclip         | NaN            | NaN            | JPN     | NaN      |

Comparing to my previous post, the above DataFrame has now two additional columns, namely the country and city Series.

My objective remains the same: add a new row for all instances where the previous company names is non-missing with the country and city columns and delete the previous names Series afterwards. Visually, the "denormalized" version should look like this:

| name   | postal_code | country | city     |
|--------|-------------|---------|----------|
| Nitron | 1410        | BEL     | Brussels |
| Rotory | 1410        | BEL     | Brussels |
| Datec  | 1410        | BEL     | Brussels |
| Pulset | 1020        | ENG     | NaN      |
| Cmotor | 1020        | ENG     | NaN      |
| Rotaxi | 1310        | JPN     | NaN      |
| Cyclip | 1310        | JPN     | NaN      |

After spending some time understanding the the code provided by jezrael for my previous question, I tried to modify/adjust the solution for this new problem without success. Since I'm fairly new to the Python/Pandas ecosystem, any additional help would be greatly appreciated.

glpsx
  • 587
  • 1
  • 7
  • 21

1 Answers1

2

You can add multiple columns in set_index and change level=1 to level=3 for remove forth level of MultiIndex:

df1 = (df.set_index(['postal_code','country','city'])
         .stack()
         .reset_index(level=3, drop=True)
         .reset_index(name='name')
         )
print (df1)
   postal_code country      city    name
0         1410     BEL  Brussels  Nitron
1         1410     BEL  Brussels  Rotory
2         1410     BEL  Brussels   Datec
3         1020     ENG       NaN  Pulset
4         1020     ENG       NaN  Cmotor
5         1310     JPN       NaN  Rotaxi
6         1310     JPN       NaN   Datec

And for second solution add multiple columns to melt:

df1 = (df.melt(['postal_code','country','city'], value_name='name')
         .drop('variable', axis=1)
         .dropna(subset=['name'])
         .reset_index( drop=True)
)
print (df1)
   postal_code country      city    name
0         1410     BEL  Brussels  Nitron
1         1020     ENG       NaN  Pulset
2         1310     JPN       NaN  Rotaxi
3         1410     BEL  Brussels  Rotory
4         1310     JPN       NaN   Datec
5         1020     ENG       NaN  Cmotor
6         1410     BEL  Brussels   Datec
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252