ironzionlion
ironzionlion

Reputation: 889

Concat DataFrame under specific condition

For the following dataframes which are stored in a list of lists, I want to concat them if there is something to:

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                     'B': ['B0', 'B1', 'B2', 'B3'],
                     'C': ['C0', 'C1', 'C2', 'C3'],
                     'D': ['D0', 'D1', 'D2', 'D3']},
                    index=[0, 1, 2, 3])

fr_list = [[] for x in range(2)]
fr_list[0].append(df1)
fr_list[0].append(df1)
fr_list[1].append(df1)

for x in range(2):
   df = pd.concat(fr_list[x] if len(fr_list[x]) > 1)  # <-- here is the problem

Upvotes: 0

Views: 40

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148880

The syntax you want is probably:

...
df = pd.concat((fr for fr in fr_list[x] if len(fr) > 1))

Upvotes: 2

Related Questions