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
  • 7,766
  • 8
  • 46
  • 61
NayyNeww
  • 39
  • 6

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
  • 729,927
  • 78
  • 1,141
  • 1,090