Home > Software design >  Given 3 series merge to create a new series with 1 column containing numbers of each dataframe
Given 3 series merge to create a new series with 1 column containing numbers of each dataframe

Time:12-04

I have 3 series with data like below

s1 = [1,1,3]
s2 = [2,3,2]
s3 = [4,2,1]

I want to create a new series with values such that

s_new = [124,132,321]

please note that s_new = int(''.join(s1,s2,s3)) I know the above syntax is wrong but you get the idea.

CodePudding user response:

You can do with pandas agg

s = pd.DataFrame([s1,s2,s3]).astype(str).agg(''.join).astype(int).tolist()
Out[334]: [124, 132, 321]

CodePudding user response:

s1 = [1,1,3]
s2 = [2,3,2]
s3 = [4,2,1]
matrix = [s1,s2,s3]
s_new = ["" for x in range(len(matrix))];

for i in range(0,len(matrix)):
    for j in range(0,len(matrix[i])):
        s_new[i] =str(matrix[j][i])
        
print(s_new)

unfortunately, I am not pyton programmer but this algo still works

CodePudding user response:

Using numpy

import numpy as np


s1 = [1, 1, 3]
s2 = [2, 3, 2]
s3 = [4, 2, 1]

s4 = [int("".join(str(i) for i in x)) for x in np.column_stack([s1, s2, s3])]
print(s4)

[124, 132, 321]
  • Related