Extract N values from 2 dataframes (pandas)

Viewed 28

i have 2 pandas dataframes with same columns [id, value].

I want to create a new dataframe extracting 200 values for each id taking instances from the first df1 and if it hasn't enough values i should go to the second df2 to take remaining values.

How can i do? Thanks

1 Answers

You can concatenate two dataframe and extract the values with groupby().head().

(pd.concat((df1, df2))
  .groupby('id').head(200)
)
Related