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.