2

I have a list of about 25 dfs and all of the columns are the same. Although the row count is different, I am only interested in the first row of each df. How can I iterate through the list of dfs, copy the first row from each and concatenate them all into a single df?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
NayyNeww
  • 39
  • 7

1 Answers1

0

Select first row by position with DataFrame.iloc and [[0]] for one row DataFrames and join together by concat:

df = pd.concat([x.iloc[[0]] for x in dfs], ignore_index=True)

Or use DataFrame.head for one row DataFrames:

df = pd.concat([x.head(1) for x in dfs], ignore_index=True)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252