Home > OS >  Python Pandas replace first occurence only
Python Pandas replace first occurence only

Time:01-08

I had asked a similar question few days back for Bash program. The solution given here worked for me. Now I plan to try the same in python. I am stuck at this similar position. So I have this csv file separated by semicolon. I need to replace first occurrence of 1 in the input of the 2nd column (named as L) into 12:30. Note 1 will be the entire string in the location. So entire string replacement should be considered.

This is my code

> import csv
> import pandas as pd
> import numpy as np
> df = pd.read_csv("grade2.csv", sep = ';')
> 
> df2=df.replace(to_replace = 1, value = "12:30")
> print(df2)
> #this code replaces all 1s to 12:30 --not what I want. 
> df3=df.replace({'L':{'1':'12:30'}})} #thought this would do. 
> #this code gives error
> print(df3))

My csv file looks like this Input data

> Name ;C;L;G;C;R
> Ben ;;14;1;;
> Ben ;;1;;1;
> Ben ;1;1;;1;
> Ben ;1;;1;1;


Output data should be 
> Name ;C;L;G;C;R
> Ben ;;14;1;;
> Ben ;;12:30;;1;
> Ben ;1;1;;1;
> Ben ;1;;1;1;

CodePudding user response:

  • Select all the values 1 in the column L.
  • Use idxmax to find the index of the first occurence (the function returns the first row label in case of multiple values equal to the max, 1 in this case).
  • Select the value to modify with loc.
df.loc[(df['L'] == 1).idxmax(), 'L'] = "12:30"

#  Name    C      L    G  C.1   R
# 0  Ben  NaN   14.0  1.0  NaN NaN
# 1  Ben  NaN  12:30  NaN  1.0 NaN
  • Related