Assign the value of another column

I have the following DataFrame in pandas:
z

And when the “Date” column is empty, I want to assign the same value (null value) to the “ID” column.
z1

If you post your DF construction code (formatted, please), it would be far easier to advise you as to how to alter the code in order to build the DF as required.

import pandas as pd
import numpy as np
data = {'ID': [1,2,3,4,5,6,7,8],
       'Date': ['20-04-2000','21-04-2000','21-04-2000','', '', '22-04-2000','','22-04-2000'],
       'Name': ['A','B','C','D','E','F','G','H']}
df = pd.DataFrame(data)
print(df)

Okay.

I’d do it like this:

import pandas as pd

dates = ['20-04-2000', '21-04-2000', '21-04-2000',
         '', '', '22-04-2000', '', '22-04-2000']

names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']

df_constructor = {'ID': [],
                  'Date': [],
                  'Name': []}

for index, date in enumerate(dates):
    if date:
        df_constructor['ID'].append(index + 1)
        df_constructor['Date'].append(date)
        df_constructor['Name'].append(names[index])
    else:
        df_constructor['ID'].append('')
        df_constructor['Date'].append('')
        df_constructor['Name'].append(names[index])

df = pd.DataFrame(df_constructor)

print(df)

thank you soo much :slight_smile:

1 Like

You are very welcome; pleased to help.