熊猫 - 如何根据另一个数据框创建新的数据框?

时间:2017-12-06 18:04:03

标签: python pandas dataframe

我有一个包含学校类型及其位置的数据框。其中一列是“Institude_Type”,两个学校类型是“中学(非语法)学校”和“中学(语法)学校”。我想学习所有中学(非语法)学校的信息并将其放入另一个数据框中 - 但我不知道该怎么做。

我希望它是当前DF的精确副本,所有相同的8列。只有一个有语法,另一个有非文法学校。

提前致谢。

2 个答案:

答案 0 :(得分:1)

使用内置复制功能:

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.copy.html

例如:

schoolCopy = schoolDataFrame[allTheColumnsRequired].copy(deep=True)

答案 1 :(得分:0)

以前的堆栈溢出答案应有帮助:

create a new dataframe from selecting specific rows from existing dataframe python

此过程需要布尔运算符,以便它仅适用于条件为true的数据框的一部分。这需要()中的[]

代码:
df2 = df[(df['Institude_Type'] == 'Secondary (non-grammar) School')]
df3 = df[(df['Institude_Type'] == 'Secondary (grammar) School')]