Home > Blockchain >  Join columns with empty values in csv, Python
Join columns with empty values in csv, Python

Time:08-03

I am joining 2 columns from a csv file and the columns look like this:

image

I need to join them in a new column and make them look like this:

image

I have tried with this code but when the steps the values remain in 'nan':

import pandas as pd

movimientosCta=pd.read_csv('file2.csv')

df = pd.DataFrame()  

df["valor"] = movimientosCta["number two"]   movimientosCta["number"] 

df.to_csv('file2.csv', index= False)

How can i solve it, thanks.

CodePudding user response:

assuming the column1 has null values. below should move the values in the right columns to the left when value in left is null

Can you also post the csv or dataframe, to help reproduce?

df.fillna(axis=1)

CodePudding user response:

You can also use a np.where()

df['All_Values'] = np.where(df['Column1'].isna(), df['Column2'], df['Column1'])
  • Related