put two columns from two different datasets together into one column

Question:

I have two dataframes

df1

name
Elias
David
Simon
Manuel

and a second df2

name
Gabriel
Brian
Simona
Danielle
Dilara
Martin
David
Simon

I one to put them into one column

I expecting an output like these:

name
Elias
David
Simon
Manuel
Gabriel
Brian
Simona
Danielle
Dilara
Martin

where every name occur once, so remove also duplicates.

i tried these

frames = [df1,df2]

but these gave me something different

Asked By: Jimmy

||

Answers:

**here we are creating two data frame by using pd.Series **

df1 = pd.Series(['a', 'b'])
df2 = pd.Series(['c', 'd'])

and using pd.concat we concatenate df1 dataframe firstly with df2 dataframe

pd.concat([df1, df2])

as you can see the output df1 show firstly and lastely the df2 :

0    a
1    b
0    c
1    d

note:

you can concatenate multiple dataframe [df1, df2,….,dfn]

Answered By: Hi Chem

Assuming the columns names are ‘A’ and ‘B’:

df3 = pd.concat([df1['A'], df2['B']], axis = 0).drop_duplicates().reset_index(drop=True)
Answered By: user19077881

You can use the pandas.concat() function to merge two columns from two different datasets into one column. Here’s an example of how you can do this:


# Select the columns you want to merge
col1 = df1['name']
col2 = df2['name']

merged_column = pd.concat([col1, col2])

merged_df = pd.DataFrame({'name': merged_column}).reset_index(drop=True)

Answered By: Hazem Hassine
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.