0

I wrote a function that outputs 3 lists and want to make those lists each a column in a dataframe.

The function returns a tuple of 3 lists, containing text or lists of text.

Here is the function:

def function(pages = 0):
    a = [title for title in range(pages)]

    b = [[summary] for summary in title.summary]

    c = [[summary2] for summary2 in title.summary2]

    return a, b, c

data = function(pages = 2)

pd.DataFrame(data, columns = ['A', 'B', 'C'])

and the error says that I passed data with 2 columns while the columns have 3 columns. Can someone explain what is going on and how to fix it? Thank you!

megansorel
  • 21
  • 3
  • We can't say for sure because we can't reproduce the same problem since we don't know what `titles` and `other_titles` look like. Always provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) when asking for help. These should be useful as well: [how to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) – Rodalm Jun 28 '22 at 22:46

1 Answers1

0

One of the way you can address this transpose the output and then create the dataframe. A sample example:

import pandas as pd
import numpy as np

def function(pages=0):
    # Replace this with your logic
    a=list(range(10))
    b=[i*0.9 for i in a]
    c=[i*0.5 for i in a]
    return [a,b,c]

data=np.array(function()).T.tolist()

df=pd.DataFrame(data=data,columns=['A','B','C'])

Output:

In []: df
Out[25]:
     A    B    C
0  0.0  0.0  0.0
1  1.0  0.9  0.5
2  2.0  1.8  1.0
3  3.0  2.7  1.5
4  4.0  3.6  2.0
5  5.0  4.5  2.5
6  6.0  5.4  3.0
7  7.0  6.3  3.5
8  8.0  7.2  4.0
9  9.0  8.1  4.5
teedak8s
  • 732
  • 5
  • 12