Home > Mobile >  Filling empty column with condition
Filling empty column with condition

Time:08-28

I wanna fill my dataset with condition, by using columns in dataset. enter image description here

CodePudding user response:

Based on your image I think apply() with a custom function f solves it for you.

def f(x):
    if x<70:
        return "Mini truck"
    elif 70<x<90: # because of the logic befor this cloud simplified to x<90
        return "VAN"
    elif 90<x<100:
        return "Bus"
    elif 100<x<120:
        return "SUV"
    elif 120<x:
        return "PickUP truck"

df["Car type"] = df["Average Hour"].apply(f)

Minimal Example

import pandas as pd
from io import StringIO

t="""Brand  Average Hour
Audi  122
BWM  89
"""

df = pd.read_csv(StringIO(t), sep="\s\s", index_col=0)
df["Car type"] = df["Average Hour"].apply(f)

>>> df 
       Average Hour      Car type
Brand                            
Audi            122  PickUP truck
BWM              89           VAN
  • Related