Quantcast
Channel: Active questions tagged row - Stack Overflow
Viewing all articles
Browse latest Browse all 448

Add new values into a row below or a newly created row below based on conditions

$
0
0

Consider this simplified df:

import pandas as pddata = { 'Name_Type': ["Primary", "Primary", "AKA", "Primary"],'Name': ["John", "Daniel", "Dan", "Bob"],'Surname': ["Green", "Brown", "Brown", "White"],'Country Type': ["Origin", "Origin", None, "Origin"],'Country': ["UK", "UK", None, "UK"],'Other': ["Info", None, None, "Info"]}df = pd.DataFrame(data)
  Name_Type    Name Surname Country Type Country Other0   Primary    John   Green       Origin      UK  Info1   Primary  Daniel   Brown       Origin      UK  None2       AKA     Dan   Brown         None    None  None3   Primary     Bob   White       Origin      UK  Info

So I want to add new values under each row that has Origin not None. If there is already a generated row with None under (like in row 2 in example), I want to add value "Citizenship" into Country Type column and value "UK" into Country of this row. If there isn't a row, I want to create a new row under the current one and add same values. So the final output will be like this:

  Name_Type    Name Surname Country Type Country Other0   Primary    John   Green       Origin      UK  Info1      None    None    None  Citizenship      UK  None2   Primary  Daniel   Brown       Origin      UK  None3       AKA  Daniel   Brown  Citizenship      UK  None4   Primary     Bob   White       Origin      UK  Info5      None    None    None  Citizenship      UK  None

Viewing all articles
Browse latest Browse all 448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>