Home > Mobile >  Merging multiple rows in a column into single row with python
Merging multiple rows in a column into single row with python

Time:12-20

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

CodePudding user response:

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(int(len(df)/4)):
    #get the current four values
    current_four = df.iloc[i*4: (i*4)   4]
    #convert the values to strings to join them together
    new_entry = ''.join(map(str, list(current_four['A1'])))
    A2.append(int(new_entry))
 
result_df = pd.DataFrame({'A2': A2})
  • Related