jm1701
jm1701

Reputation: 61

copy rows of pandas dataframe to a new dataframe

i have a dataframe almost like this one :

id name numOfppl
1  A     30
2  B     31
3  C     10
4  D     0
.
.
.
31  comp  52

These numbers are coming from a python code. Once we have 5 rows where numOfppl >=30, the code will stop and return all the rest of the rows to a new dataframe.

my code so far:

df[df['numOfppl'] >= 30].iloc[:5]

if more rows are added, how can i copy them to a new Dataframe ?

Upvotes: 0

Views: 1343

Answers (1)

Abhay
Abhay

Reputation: 615

Once you have created a dataframe for the condition you mentioned, you need all the other rows to be in a new dataframe, right?
Please check with the below

df_1 = df[df['numOfppl'] >= 30].iloc[:5]
df_2 = df[~df.isin(df_1)].dropna()

Here df_1 will have 5 rows as you mentioned with the condition and rest all the rows will be copied to df_2.
Also, for newly added rows (later) you can directly copy them into dataframe df_2

Upvotes: 2

Related Questions