1

So I am working on the below dataframe:

DF

What I am trying to do is merge the numeric values of 2 columns ('elected_in' & 'campaigned_in_') into a new column.

This column should look like this

new_column

  • 007
  • NaN
  • 043
  • 275
  • 027
  • etc

Any tips on how to do this at all? Found all the stack overflow answers not quite pertaining to this and I am also not sure what terminology to use...

Thanks for your help in advance.

chefbilby
  • 35
  • 3

3 Answers3

2

Iterate over rows using iterrows() method and replace value on condition (if value in campaigned_in is a string replace with that from elected_in)

import pandas as pd
df = pd.DataFrame({"elected_in" : [0.07, "Bremen", "Nied"]})
df['campaigned_in'] = ["Schleswig",45,275]
df["answer"]=df["campaigned_in"]
for index, row in df.iterrows(): 
  if(isinstance(row["campaigned_in"],str)==True):
    row["answer"]=row["elected_in"]
df.head()

Updated df look like:

    elected_in  campaigned_in   answer
0   0.07        Schleswig       0.07
1   Bremen      45              45
2   Nied        275             275
Vidya Ganesh
  • 788
  • 11
  • 24
1

You can basically combine them using new column. I'm suggesting this as you have number and string both as value in the columns that you're trying to merge. Please refer below code.

import pandas as pd

df = pd.DataFrame(np.array([[7, "Bremen", "test"], [4, 5, 6], ["trial", 8, 43]]),
                   columns=['elected', 'b', 'campained'])

enter image description here

# Now combine them
df['number'] = df['elected'] + " " +df['campained']
df.head()

enter image description here

If you only want numbers then you can use simple lambda function to do that.

import re as re

def find_number(text):
    num = re.findall(r'[0-9]+',text)
    return " ".join(num)

df['new']=df['number'].apply(lambda x: find_number(x))
df.head()

enter image description here

Jay Patel
  • 1,374
  • 1
  • 8
  • 17
1

Edit: Changed so that output is string format

def merge(e,c):
    if str(e).isnumeric(): 
        return e     
    elif str(c).isnumeric():
        return c
    else:
        return np.nan

data = {'elected_in':['007', 'Bremen', 'test1', 182],
         'campaigned_in_':['sh-h', np.nan, '043', 'test2']
       }
df = pd.DataFrame(data)
df['new_column'] = df.apply(lambda x: merge(x.elected_in, x.campaigned_in_), axis = 1)

output:

elected_in  campaigned_in_  new_column
0   007        sh-h         007
1   Bremen     NaN          NaN
2   test1      043          043
3   182        test2        182
Ajay Rawat
  • 258
  • 2
  • 6