2

I have a dataframe that contains school types and their locations. One of the columns is "Institude_Type" and the two school types are "Secondary (non-grammar) School" and "Secondary (grammar) School". I want to take all of the Secondary (non-grammar) School's information and put it into another dataframe - but I'm not sure how to do this.

I want it to be an exact copy of the current DF, with all the same 8 columns. Just one with the grammar and the other with non grammar schools.

Thanks in advance.

Ash
  • 73
  • 1
  • 1
  • 7
  • Hope this helps https://stackoverflow.com/questions/40455457/how-to-create-a-new-data-frame-based-on-conditions-from-another-data-frame – metadata Dec 06 '17 at 18:07
  • Also read this 10 min introduction to pandas. It'll give you all the basic functions of pandas - https://pandas.pydata.org/pandas-docs/stable/10min.html – TrigonaMinima Dec 06 '17 at 18:16
  • Possible duplicate of [pandas: Extracting specific selected columns from a DataFrame to new DataFrame](https://stackoverflow.com/questions/34682828/pandas-extracting-specific-selected-columns-from-a-dataframe-to-new-dataframe) – MattR Dec 06 '17 at 18:31

2 Answers2

2

use the built-in copy function:

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

For example:

schoolCopy = schoolDataFrame[allTheColumnsRequired].copy(deep=True)
BENY
  • 317,841
  • 20
  • 164
  • 234
Omar
  • 329
  • 1
  • 3
  • 10
1

This previous Stack Overflow answer should help:

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

This process requires a Boolean operator, such that it only applies to a portion of the dataframe where the condition is true. This requires () within the [].

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

mcbridecaleb
  • 101
  • 1
  • 8