'Concat null columns data with actual data in pandas?
Solution 1:[1]
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)
Solution 2:[2]
using apply method we can merge these columns, as below;
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Maharajaparaman |
Solution 2 |