Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merge two dataframe columns into 1 in pandas

I have 2 columns in my data frame and I need to merge it into 1 single column

Index  A             Index   B
0      A             0       NAN
1      NAN           1       D
2      B             2       NAN
3      NAN           3       E
4      C             4       NAN

there will always be one 1 value across the Columns, i want my result to look like

0    A
1    B
2    C
3    D
4    E
like image 416
viper Avatar asked Sep 01 '16 16:09

viper


People also ask

How do I convert multiple columns to single column in pandas?

Step #1: Load numpy and Pandas. Step #2: Create random data and use them to create a pandas dataframe. Step #3: Convert multiple lists into a single data frame, by creating a dictionary for each list with a name. Step #4: Then use Pandas dataframe into dict.

How do I combine column values in pandas?

To start, you may use this template to concatenate your column values (for strings only): df['New Column Name'] = df['1st Column Name'] + df['2nd Column Name'] + ... Notice that the plus symbol ('+') is used to perform the concatenation.

How to merge two columns in pandas?

How to Merge Two Columns in Pandas ? : 3 Steps Only Step 1: Import the Necessary Packages Numpy and Pandas Packages are only required for this tutorial, therefore I am... Step 2: Create a Dataframe For the demonstration purpose, I am creating a Dataframe manually. You can apply the same... Step 3: ...

How to join two DataFrames in pandas Dataframe?

Let us see how to join two Pandas DataFrames using the merge () function. Returns : A DataFrame of the two merged objects. Example 2 : Merging two Dataframe with different number of elements : If we use how = "Outer", it returns all elements in df1 and df2 but if element column are null then its return NaN value.

How to combine two text columns into one in a Dataframe?

You can use the following syntax to combine two text columns into one in a pandas DataFrame: df[' new_column '] = df[' column1 '] + df[' column2 '] If one of the columns isn’t already a string, you can convert it using the astype(str) command: df[' new_column '] = df[' column1 ']. astype (str) + df[' column2 ']

How do I join a named series to a Dataframe?

A named Series object is treated as a DataFrame with a single named column. The join is done on columns or indexes. If joining columns on columns, the DataFrame indexes will be ignored.


Video Answer


1 Answers

Option 1

df.stack().dropna().reset_index(drop=True)

0    A
1    D
2    B
3    E
4    C
dtype: object

Option 2 If Missing values are always alternating

df.A.combine_first(df.B)

Index
0    A
1    D
2    B
3    E
4    C
Name: A, dtype: object

Option 3 What you asked for

df.A.append(df.B).dropna().reset_index(drop=True)

0    A
1    B
2    C
3    D
4    E
dtype: object

Option 4 Similar to 3 but over arbitrary number of columns

pd.concat([i for _, i in df.iteritems()]).dropna().reset_index(drop=True)

0    A
1    B
2    C
3    D
4    E
dtype: object
like image 180
piRSquared Avatar answered Oct 25 '22 03:10

piRSquared