Merging multiple rows in a column into single row with python

Question:

I wanted to merge every four rows in a column into a single row in the next column. For the following dataframe, it will convert 16 rows to 4 rows where each row contains values.

df = pd.DataFrame({
    'A1': [1,0,0,0,1,1,0,0,1,0,0,0,1,1,1,0]})

output should be,

A2:
1000
1100
1000
1110
Asked By: Rupak Paul

||

Answers:

Note: Since the second column will only have 4 rows but the first will have 16, there will be a dimension mismatch. So you will either have to save the second column in a second dataframe or repeat the A2 label for each A1 value

Besides that, this should work to get the A2 values you were looking for.

import pandas as pd
import numpy as np


df = pd.DataFrame({
    'A1': [1,0,0,0,1,1,0,0,1,0,0,0,1,1,1,0]})

A2 = []

#loop through every fourth value
for i in range(0, len(df), 4):
    #get the current four values
    current_four = df.iloc[i: (i + 4)]

    #convert the values to strings to join them together
    new_entry = ''.join(map(str,list(current_four['A1'])))
    A2.append(new_entry)

result_df = pd.DataFrame({'A2': A2})
 
Answered By: user15278741
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.