This is the continuation of my previous post on denormalizing a DataFrame of company names.
SOME BACKGROUND:
Initially, I was working with the table below:
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 |
The goal of the denormalization was to add a new row for all instances where the previous company name was non-missing and delete all previous name Series afterwards.
Thanks to jezrael, I was able to achieve this with the following code:
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
NEW GOAL
My new goal is to add an additional flag/column with values defined as
- 1 if the company name is an old one (i.e. from one of the previous name Series);
- 0 otherwise.
That is, the new DataFrame should visually look like this (the order of the columns do not matter):
| name | postal_code | country | city | old_name_flag |
|--------|:-----------:|:-------:|----------|:-------------:|
| Nitron | 1410 | BEL | Brussels | 0 |
| Rotory | 1410 | BEL | Brussels | 1 |
| Datec | 1410 | BEL | Brussels | 1 |
| Pulset | 1020 | ENG | NaN | 0 |
| Cmotor | 1020 | ENG | NaN | 1 |
| Rotaxi | 1310 | JPN | NaN | 0 |
| Cyclip | 1310 | JPN | NaN | 1 |
I tried to adjust the code of jezrael without success. Any additional help would be greatly appreciated.