0

I have a function that produces a dataframe with 10 rows and i my goal is to get 135 of these dataframes. I don't want them separate, so I need to concatenate them into one dataframe so that it has 10 rows and 135 labeled columns, but i want to do this efficiently. The closest i got to what i needed was this:

for i in range(len(docs)):
    for l in labels_list:
        df = pd.concat([pd.DataFrame({l:(my_func(i)})])

But obviously this just returns the very last column of the dataframe...

EDIT

Another way:

I appended the result of my_func into a list and i now have a list of lists with 135 lists and 10 strings in each list. I want to make a dataframe out of this with 135 columns and 10 rows, but the regular way gives me an error:

df2 = pd.DataFrame(list_of_df, columns=labels_list)

ValueError: 135 columns passed, passed data had 10 columns
mojbius
  • 95
  • 6

2 Answers2

1

Try:

pd.concat([pd.DataFrame({labels_list[i]: my_func(i)}) for i in range(len(docs))], axis=1)

If you already have the list of dataframes:

df = pd.concat(list_of_df, axis=1)
df.columns = labels_list
Cainã Max Couto-Silva
  • 4,839
  • 1
  • 11
  • 35
0

If I understand you correctly your function my_func returns the type pd.DataFrame. Your goal is to have 135 columns and each column has the name l from labels_list. You need to ensure that you will have unique column names as well. The format string "{} {}".format(str(i), l) tries to achieve this. You need to be sure that you convert i to a str. I'm indicating that with the str() function here.

Then I would do it like this:

for i in range(len(docs)):
    for l in labels_list:
        df["{} {}".format(str(i), l)] = my_func(i).iloc(:,0)

As your function my_func has only one column you can also access it by it's name, so like this:

df[l] = my_func(i)["column_name"]

I hope this helps. If not please be more specific about your function and try to have a complete minimal working example. Your problem also seems to be quite similar to this stackoverflow question.

EDIT: Check out this stackoverflow question for your question edit. That might help.

flyingdutchman
  • 1,197
  • 11
  • 17
  • this created a 135x135 df and with the same entries (last output of my_func) in every column.. – mojbius Nov 26 '20 at 10:54
  • Then add an assertion like so `assert len(my_func(i)["column_name"]) == 10` to be sure that every column has actually 10 elements/rows. – flyingdutchman Nov 26 '20 at 10:59
  • what about the repeating values? – mojbius Nov 26 '20 at 11:02
  • Then try to create a [minimum working example](https://stackoverflow.com/help/minimal-reproducible-example) and edit your question accordinly. As I don't know what is happening in your function `my_func` and what `docs` and `labels_list` is I can't help you any further now. – flyingdutchman Nov 26 '20 at 11:12