0

to update: use the @azro code and modify only the indexes of the for. it doesn't mark me error with your answers, the code works fine, however I still don't get the expected result, I put my full code.

arch is dataframe
channels=['ch1','ch1']
targets=['op1','op2']
l=[]
m = []
n = []
for ch in channels:
    for tar in targets:
        for j in range(1,22):                                                        
            df=arch[(arch.Año.isin(año)) & (arch['Channel'] == ch) & (rcha['Week'] == j)]

           l.append(df[['time',tar]].set_index('time').rename(columns={'time' : 'time' + str(j)}))
        m.append(l)
     n.append(m)

a = []
for i in range(len(n)):
    for j in range(len(m)):
        for k in range(len(l)):
            a.append(n[i][j][k])
a = pd.concat(a, axis=1).reset_index()

by printing "a" I get

time | op1 | op1 |...| op2 | op2 |..| op1 | op1 | op2 |..| op2 
1    | 0.2 | 0.1 |   | 0.2 | 0.1 |..| 0.1 | 0.1 | 0.2 |..| 0.8
2    | 0.3 | 0.4 |   | 0.1 | 0.3 |..| 0.2 | 0.7 | 0.9 |..| 0.3
3    | 0.7 | 0.8 |   | 0.9 | 0.11|..| 0.4 | 0.8 | 0.7 |..| 0.8

I have it like this, because I have two elements in "channels" and two elements in "targets" and I need to print a table for "ch1 with op1", "ch1 with op2", "ch2 with op1", "ch2 with op2".

for ch1
time | op1 | op1 |..
1    | 0.2 | 0.1 |..  
2    | 0.3 | 0.4 |..
3    | 0.7 | 0.8 |

time | op2 | op2 |..
1    | 0.2 | 0.1 |..
2    | 0.1 | 0.3 |..
3    | 0.9 | 0.11|.. 


for ch2

time | op1 | op1 | ..
1    | 0.1 | 0.1 | ..
2    | 0.2 | 0.7 | ..
3    | 0.4 | 0.8 | ..

time | op2 |..| op2 
1    | 0.2 |..| 0.8
2    | 0.9 |..| 0.3
3    | 0.7 |..| 0.8
1
  • As the error states, pd.concat expects to concatenate multiple things, but you passed it a single DataFrame. Perhaps a = pd.concat(np.flatten(n), axis=1).reset_index()? Commented Jul 29, 2020 at 21:00

3 Answers 3

0

I think you are trying to do something like this:

a=n[0][0][0]
for i in range(len(n)):
    for j in range(len(n)):
        for k in range(len(n)):
            if not i==j==k==0:
                a=pd.concat((a, n[i][j][k]),axis=1).reset_index()

'a' is your cumulative concatenation, so you then need to concatenate that with the next entry

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, your answer solves the error, but I still don't get the desired result, I have already updated my code to see if it is clearer
0

You need to pass multiple Dataframe to concat not just one, you'd better to it in one-shot

a = []
for i in range(len(n)):
    for j in range(len(n)):
        for k in range(len(n)):
            a.append(n[i][j][k])
a = pd.concat(a, axis=1).reset_index()

1 Comment

thanks, your answer solves the error, but I still don't get the desired result only the first two values ​​concatenate me, not all , I have already updated my code to see if it is clearer
0

If I understand it correctly, you want this:

import numpy as np
a = pd.concat(np.flatten(np.array(n)), axis=1).reset_index()

If this does not work, please share with us a small sample of your n array to make it work.

2 Comments

thanks, your answer solves the error, but I still don't get the desired result, I have already updated my code to see if it is clearer
@KLM Please provide a sample input n and desired output a so we understand better. The code you updated (which I assume does not achieve what you desire) does not make it clearer. Thank you. And what is channels and arch in your updated code? Please make it reproducible

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.