简体   繁体   English

将 pandas dataframe 的行复制到新的 dataframe

[英]copy rows of pandas dataframe to a new dataframe

i have a dataframe almost like this one:我有一个 dataframe 几乎像这个:

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.这些数字来自 python 代码。 Once we have 5 rows where numOfppl >=30, the code will stop and return all the rest of the rows to a new dataframe.一旦我们有 5 行 numOfppl >=30,代码将停止并将行的所有 rest 返回到新的 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?如果添加了更多行,我如何将它们复制到新的 Dataframe?

Once you have created a dataframe for the condition you mentioned, you need all the other rows to be in a new dataframe, right?为您提到的条件创建 dataframe 后,您需要所有其他行都在新的 dataframe 中,对吗?
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.这里 df_1 将有 5 行,正如您在条件中提到的那样,rest 所有行都将复制到 df_2。
Also, for newly added rows (later) you can directly copy them into dataframe df_2此外,对于新添加的行(稍后),您可以直接将它们复制到 dataframe df_2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM