1

I have a Excel sheet with 10000 Rows. I have to change the value of a Colum5 based on value of Colum2 and update the excel sheet.

I am able to change the values using:

import numpy as np
df=pd.read_excel('part2.xlsx')
count=0

while (count<10000):
     if df['Grade'][count] == 'A' :
         #df.loc[df['Grade'][count]] = 'Good'
         df['Grade'][count] = 'Good'
         print(count)
         print(df['Grade'][count])
     count=count+1

df.to_excel('temp.xlsx')

The issue with this is as I am writing to a new Excel sheet all the formatting is gone. I want to achieve the same thing in the same excelsheet so that format is not removed.

2) is there any use in keeping the df.loc here?

user
  • 35
  • 1
  • 7

1 Answers1

0

By using .loc

df=pd.DataFrame({'Grade':['A','B','C','A','D']})
df.loc[(df.Grade=='A')&(df.index<3),'Grade']='Good'
df
Out[489]: 
  Grade
0  Good
1     B
2     C
3     A
4     D

More info

(df.Grade=='A')&(df.index<3)
Out[296]: 
0     True
1    False
2    False
3    False
4    False
Name: Grade, dtype: bool
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thankyou , I di dnot get how this loc is working here? dont I need to write to excel? can you please explain bit more – user Feb 02 '18 at 00:39
  • @user pandas dataframe is base on index, first we get the row need to change by this Boolean index getting the all A before certain index. (df.Grade=='A')&(df.index<3), then we block the top 3 (in your real case should be 10000), then we apply the change, also add more info – BENY Feb 02 '18 at 01:25
  • when we write the frame to the excelsheet the formating will be lost. As shown in question can u please let me understand how to achieve this – user Feb 03 '18 at 00:15