luka
luka

Reputation: 529

Split dataframe based on empty column condition

I have this kind of dataframe, I want to split it into two dataframe if the "FT" column is empty

df

    HomeTeam    AwayTeam         FT
0   Colo Colo   U. De Concepcion    
1   Cobresal    U. Espanola 
2   Deportes    S. Wanderers    
3   La Serena   A. Italiano      A
4   O'Higgins   Colo Colo        D
5   Palestino   Coquimbo         D

Tried this code but df still the same

if "" in df['FT']:
    df1 = df[df['FT'] == '']
    df = df[df['FT'].notna("")]

Upvotes: 2

Views: 809

Answers (1)

Rutger
Rutger

Reputation: 603

It looks like the column is not missing (na) but has as value '' (empty string). In that case you can do:

df_missing = df.loc[df['FT'] == "", :]
df_not_missing = df.loc[df['FT'] != "", :]

The .loc allows you to select rows for which the values match some criteria, columns for which the values match some criteria or both. The first indexer is for selecting rows, the second (optional) for selecting columns.

Upvotes: 3

Related Questions