简体   繁体   中英

Need help using Loc to bin and replace numbers

I have a column with number values. I want to determine if the number in the column is above or below the number 30 and replace it with a 1 if its above and 0 if its below. I am currently using.loc but I can't seem to figure out the | annotation feature

    kraken = pd.read_csv(data_path + 'ITSM_Master_2020-10-05.csv')
    kraken.loc[[kraken['Incident_Duration']<30, 'high_or_low']=0 | 
    [kraken['Incident_Duration']>=30, 'high_or_low']=1]

something like that? I am putting the numbers into a column called high or low Incident Duration is a large column with numbers ranging from 0-100 for example

  Incident Duration = [25,26,50,52,50,100,5]

Some issues:

  1. Your syntax inside loc . You are passing a list with a dataframe and a string inside of it, I'm not even sure to be honest what's going on there. I'll show below how I usually work with loc , but perhaps you are not wrong I'm just not sure the source of your problem.
  2. You don't need | between the two assignments.
  3. Followed by 2. You should initiate the column 'high_or_low' before assigning values to it using loc . So let's initiate it with the values 0, and place 1 wherever needed by your condition.

Solution:

kraken['high_or_low'] = 0
kraken.loc[kraken['Incident_Duration'] >= 30, 'high_or_low'] = 1

There is no need to use loc . If you just do kraken["Incident_Duration"] >= 30 you get a boolean series that tells you, for each row, whether the condition was met. So all that is left is to convert that to 0 or 1 and append the series as a column of your data frame. You have two options for that:

  • One is to cast the series to int using pandas' astype() method:
kraken["high_or_low"] = (kraken["Incident_Duration"] >= 30).astype(int)
  • The other option is to multiply by 1, which causes pandas to cast it to int :
kraken["high_or_low"] = (kraken["Incident_Duration"] >= 30) * 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM