vendredi 29 mai 2020

Fill new pandas df based off values in another df

I am new here, so please don't be to hard on me! :)

See picture below!

Print screen: df['Datan]

I am trying to create a new dataframe (df['New_df']) based on the values in df['Datan'] so that df['New_df'] is equal to df['Datan'] on the rows where the string #SRU appears. If the string is not in df['Datan'], I want df['New_df'] to "keep" the value of the row above (where the #SRU string was included).

See below df of what I am trying to do.

| Index | Datan | New_df |
| 95 | #SRU 1512 7251 | #SRU 1512 7251 |
| 96 | #KONTO 1513 "Kundfordringar - delad faktura" | #SRU 1512 7251 |
| 97 | #SRU 1513 7251 | #SRU 1513 7251 |
| 98 | #KONTO 1519 "Nedskrivning av kundfordringar" | #SRU 1513 7251 |
| 99 | #SRU 1519 7251 | #SRU 1519 7251 |

I have been trying around with for loops combined with if statements with the apply method in particular but haven't reached a solution as of yet. Not able to find this specific problem in any other threads here.

Aucun commentaire:

Enregistrer un commentaire