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?
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.