Click here to Skip to main content
15,868,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following DataFrame in pandas: ID, Date, Name
ID    Date      Name     
1  20-04-2000     A      
2  21-04-2000     B      
3  21-04-2000     C     
4                 D      
5                 E      
6  22-04-2000     F      
7                 G       
8  22-04-2000     H    


And when the “Date” column is empty, I want to assign the same value (null value) to the “ID” column.
ID    Date      Name     
1  20-04-2000     A      
2  21-04-2000     B      
3  21-04-2000     C     
                  D      
                  E      
6  22-04-2000     F      
                  G       
8  22-04-2000     H    


What I have tried:

Despite my best efforts, I have not been successful.
Posted
Updated 20-Jan-23 2:13am

1 solution

When I created a frame like yours, the date columns contained NaN, so this code makes allowances for that. My frame is named 'table' in the following code:
Python
print('Original:', table)
for index, row in table.iterrows():
    if index == 0:
        continue
    if row[1] == '' or pd.isna(row[1]):
        table.loc[index][0] = ''
        table.loc[index][1] = ''
print('\n\nmodified:', table)

And the output is:
Original:     0           1     2
0  ID        Date  Name
1   1  20-04-2000     A
2   2  21-04-2000     B
3   3  21-04-2000     C
4   4         NaN     D
5   5         NaN     E
6   6  22-04-2000     F
7   7         NaN     G
8   8  22-04-2000     H


modified:     0           1     2
0  ID        Date  Name
1   1  20-04-2000     A
2   2  21-04-2000     B
3   3  21-04-2000     C
4                     D
5                     E
6   6  22-04-2000     F
7                     G
8   8  22-04-2000     H
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900