0

I'm considering a Pandas Dataframe. I would like to find an efficient way in which the second Dataframe is created every time the name of a column contains the number 4 and therefore column elements are arrays.

 import pandas as pd
 data = {"A4aa":[[1, 2,3,4]], "B":[12,],"B4bb":[[5, 6,7,8]]} 
 data1 = {"A0aa":[1],"A1aa":[2],"A2aa":[3],"A3aa":[4], "B":[12,],"B0bb":[5],"B1bb":[6],"B2bb":[7],"B3bb":[8]}  
 df = pd.DataFrame(data) 
 df1 = pd.DataFrame(data1)
        A4aa         B        B4bb
0   [1, 2, 3, 4]    12  [5, 6, 7, 8]
data1 = {"A0":[1],"A1":[2],"A2":[3],"A3":[4], "B":[12,],"B0":[5],"B1":[6],"B2":[7],"B3":[8]} 
 df1 = pd.DataFrame(data1)
   A0aa A1aa  A2aa  A3aa   B  B0bb  B1bb  B2bb   B3bb
0   1     2   3      4    12   5    6      7     8  

1 Answer 1

1
def f(df):
    def g(c):
        pre, *suf = c.split('4')
        if suf:
            return pd.DataFrame(df[c].tolist()).add_prefix(pre).add_suffix(''.join(suf))
        else:
            return df[c]
    return pd.concat(map(g, df), axis=1)        

f(df)

   A0aa  A1aa  A2aa  A3aa   B  B0bb  B1bb  B2bb  B3bb
0     1     2     3     4  12     5     6     7     8
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I simplified the problem too much and the solutions are not overlapping, I'm trying to adapt it. If by chance I could post an efficient solution with this new complication it would be perfect
@StefanoBarone try that
Thank you very much a little more elegant but more or less the same solution that I was able to get from your first answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.