@Lowin Li
is not set the df["Phone2"], and import numpy
you can use apply
like @Lowin Li
import pandas as pd
dict = [
{"id":"0","Phone1":"9073370404",},
{"id":"1","Phone1":"9072765522","Phone2":"1234567890"},
{"id":"2","Phone2":"9075611434"},
{"id":"3","Phone1":"9073370404",},
{"id":"4","Phone2":"9072765522"},
{"id":"5","Phone1":"9075611434",}
]
df = pd.DataFrame.from_dict(dict)
def my_conbin(a, b):
if pd.isna(a):
return b
return a
def my_delete(a, b):
if a == b:
return None
return b
df['Phone1'] = df.apply(lambda row: my_conbin(row['Phone1'], row['Phone2']), axis=1)
df['Phone2'] = df.apply(lambda row: my_delete(row['Phone1'], row['Phone2']), axis=1)
print(df)
and df.iterrows()
is more simple:
import pandas as pd
dict = [
{"id":"0","Phone1":"9073370404",},
{"id":"1","Phone1":"9072765522","Phone2":"1234567890"},
{"id":"2","Phone2":"9075611434"},
{"id":"3","Phone1":"9073370404",},
{"id":"4","Phone2":"9072765522"},
{"id":"5","Phone1":"9075611434",}
]
df = pd.DataFrame.from_dict(dict)
for index,row in df.iterrows():
if pd.isna(row.Phone1):
df.loc[index, 'Phone1']= row.Phone2
df.loc[index, 'Phone2'] = None
print(df)