Home > Enterprise >  choose rows to another dataframe and drop rows by conditional in column pandas
choose rows to another dataframe and drop rows by conditional in column pandas

Time:11-14

I have simple dataframe and i would like separate it.

Make Model Year
BMW 1 serie 2007
Kia K7 2012
BMW 6 serie 1982
BMW 6 serie 1987
BMW X3 2006
Kia Bongo 2000

i need take cars where (Year >= 2000) and put it to another dataframe, at the same time i would like leave the rest of the data (Year < 2000). No use inplace = True because as far as I know it is supposed to be removed from pandas. I did it using .loc but is there a better solution? my solution:

import pandas as pd

cars = {'Make': {0: 'BMW', 1: 'Kia', 2: 'BMW', 3: 'BMW', 4: 'BMW', 5: 'Kia'}, 
        'Model': {0: '1 serie', 1: 'K7', 2: '6 serie', 3: '6 serie', 4: 'X3', 5: 'Bongo'}, 
        'Year': {0: 2007, 1: 2012, 2: 1982, 3: 1987, 4: 2006, 5: 2000}}

df = pd.DataFrame.from_dict(cars)


df_2000 = df.loc[df["Year"]>=2000]
df = df.loc[df["Year"]<2000]

CodePudding user response:

You don't need to use loc, just filter like this:

df_2000 = df[df.Year >= 2000]
df = df[df.Year < 2000]

or:

df_2000 = df[df["Year"] >= 2000]
df = df[df["Year"] < 2000]

CodePudding user response:

For your use case you can take advantage of the pandas.DataFrame.query function :

df_2000 = df.query("Year >= 2000")
df = df.query("Year < 2000")

For Simple cases it provides easier and cleaner code.

You can read more about the pros and cons of query in this answer.

  • Related