I have set of columns need to be merged into single column where some columns have data and some don't have where it should be joined with the data to single column
Input is like this:
output :
CodePudding user response:
cols = ['Value1', 'Value2', 'Value3', 'Value4', 'Value5']
df['Value']=df[cols].apply(lambda x: x.to_string(index =False,na_rep=''),axis=1).replace({"\n":''},regex=True)
df.drop(cols, axis=1, inplace=True)
df
CodePudding user response:
You should provide your thoughts and tries. What are you thinking and how have you tried to solve this problem..
CodePudding user response:
for a simple concatenation, you can use the below code
# Import pandas library
import pandas as pd
# initialize list of lists
data = [['tom', ''], ['nick', 'vuchic'], ['juli', '']]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'LastName'])
print(df)
df['FullName']=df['Name'] ' ' df['LastName']
# print dataframe.
print(df)


