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?
Asked
Active
Viewed 141 times
2
-
1@DYZ - OP need something else, because partly dupe reopened. – jezrael Jun 03 '20 at 07:17
1 Answers
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 DataFrame
s:
df = pd.concat([x.head(1) for x in dfs], ignore_index=True)

jezrael
- 822,522
- 95
- 1,334
- 1,252
-
-
@NayyNeww - yes, it select first row, not important what is index values. – jezrael Jun 03 '20 at 07:02