Python: Copy a Row Value and Add to Another Row where a cell is empty
Question:
Trying to figure out how to do this in pandas but currently can’t figure it out.
I would like to copy a value from Col A where a cell in Col B is blank and add it to the below rows, only until it reaches the next blank cell in Col B where it will then start again.
My Python isn’t the strongest so any pointers would be appreciated as i currently haven’t got a clue where to start with this one! I have included the below as an example as to how it currently is and as to how I’d like it. I’m currently just manipulating and cleaning the data in Pandas.
A
B
Supply Voltage
BLANK
Rated Value
10
Limit
20
Size
BLANK
Height
10
Width
20
A
B
Supply Voltage
BLANK
Supply Voltage – Rated Value
10
Supply Voltage – Limit
20
Size
BLANK
Size – Height
10
Size – Width
20
Alessandro answers the original question perfectly however in my case the data is something like this in my format. Where there are boolean Yes/No and unique values mixed in. Would a groupby and fill still work in this case?
A
B
Supply Voltage
BLANK
Rated Value
10
Limit
20
Work
Yes
Size
BLANK
Height
11
Depth
14
Width
55
Description
BLANK
Time
1432
Date
10/12/2022
Quote
Hello World
Answers:
Below you can find a working example:
import pandas as pd
import numpy as np
# Recreate example DataFrame
df = pd.DataFrame({
'A': ['Supply Voltage', 'Rated Value', 'Limit', 'Size', 'Height', 'Width'],
'B': [np.nan, 10, 20, np.nan, 10, 20]
})
# Add helper column (UPDATE)
l = []
c = 0
for i in df['B']:
if pd.isnull(i):
c = c+1
l.append(c)
df['C'] = l
# Extract names associated with blank cells
blank_names = df.loc[df['B'].isna(), ['A', 'C']]
blank_names.columns = ['BLANK_NAME', 'C']
# Add names associated with blank cells to original DataFrame
df = df.merge(blank_names, on='C', how='left')
df['A'] = np.where(df['B'].notna(), df['BLANK_NAME'] + ' - ' + df['A'], df['A'])
# Display final output
df = df.drop(columns=['C', 'BLANK_NAME'])
df
Trying to figure out how to do this in pandas but currently can’t figure it out.
I would like to copy a value from Col A where a cell in Col B is blank and add it to the below rows, only until it reaches the next blank cell in Col B where it will then start again.
My Python isn’t the strongest so any pointers would be appreciated as i currently haven’t got a clue where to start with this one! I have included the below as an example as to how it currently is and as to how I’d like it. I’m currently just manipulating and cleaning the data in Pandas.
A | B |
---|---|
Supply Voltage | BLANK |
Rated Value | 10 |
Limit | 20 |
Size | BLANK |
Height | 10 |
Width | 20 |
A | B |
---|---|
Supply Voltage | BLANK |
Supply Voltage – Rated Value | 10 |
Supply Voltage – Limit | 20 |
Size | BLANK |
Size – Height | 10 |
Size – Width | 20 |
Alessandro answers the original question perfectly however in my case the data is something like this in my format. Where there are boolean Yes/No and unique values mixed in. Would a groupby and fill still work in this case?
A | B |
---|---|
Supply Voltage | BLANK |
Rated Value | 10 |
Limit | 20 |
Work | Yes |
Size | BLANK |
Height | 11 |
Depth | 14 |
Width | 55 |
Description | BLANK |
Time | 1432 |
Date | 10/12/2022 |
Quote | Hello World |
Below you can find a working example:
import pandas as pd
import numpy as np
# Recreate example DataFrame
df = pd.DataFrame({
'A': ['Supply Voltage', 'Rated Value', 'Limit', 'Size', 'Height', 'Width'],
'B': [np.nan, 10, 20, np.nan, 10, 20]
})
# Add helper column (UPDATE)
l = []
c = 0
for i in df['B']:
if pd.isnull(i):
c = c+1
l.append(c)
df['C'] = l
# Extract names associated with blank cells
blank_names = df.loc[df['B'].isna(), ['A', 'C']]
blank_names.columns = ['BLANK_NAME', 'C']
# Add names associated with blank cells to original DataFrame
df = df.merge(blank_names, on='C', how='left')
df['A'] = np.where(df['B'].notna(), df['BLANK_NAME'] + ' - ' + df['A'], df['A'])
# Display final output
df = df.drop(columns=['C', 'BLANK_NAME'])
df