0

I am trying to use a while loop to read through all the rows of my file and edit the value of a particular cell when a condition is met.

My logic is working just fine when I am reading data from an excel. But same logic is not working when I am reading from a csv file.

Here is my logic to read from Excel file:

df = pd.read_excel('Energy Indicators.xls', 'Energy', index_col=None, na_values=['NA'], skiprows = 15, skipfooter = 38, header = 1, parse_cols ='C:F')

df = df.rename(columns = {'Unnamed: 0' : 'Country', 'Renewable Electricity Production': '% Renewable'})
df = df.drop(0, axis=0)

i = 0 
while (i !=len(df)):
    if df.iloc[i]['Country'] == "Ukraine18":
        print(df.iloc[i]['Country'])
        df.iloc[i]['Country'] = 'Ukraine'
        print(df.iloc[i]['Country'])
    i += 1
df

The result I get is:

Ukraine18
Ukraine

But when I read a CSV file:

df = pd.read_csv('world_bank.csv', skiprows = 4)
df = df.rename(columns = {'Country Name' : 'Country'})
i = 0 
while (i !=len(df)):
    if df.iloc[i]['Country'] == "Aruba":
        print(df.iloc[i]['Country'])
        df.iloc[i]['Country'] = "Arb"
        print(df.iloc[i]['Country'])
    i += 1
df

The result I get is:

Aruba
Aruba

Can someone please help? What am I doing wrong with my CSV file?

1
  • I think, problem is in raw df.iloc[i]['Country'] = "Arb", you can try use df['Country'][i] or something else. Commented Aug 23, 2018 at 6:52

2 Answers 2

2

@Anna Iliukovich-Strakovskaia, @msr_003, you guys are right! I changed my code to df['ColumnName][i], and it worked with the CSV file. But it is not working with Excel file now.

So, it seems with data read from CSV file, df['ColumnName][i] works correctly, but with data read from Excel file, df.iloc[i]['ColumnName'] works correctly.

At time point, I have no clue why there should be a difference, because I am not working with the data 'within' the files, rather I am working on data that was read from these files into a 'dataframe'. Once the data is in the dataframe, the source shouldn't have any influence, I think.

Anyway, thank you for your help!!

Sign up to request clarification or add additional context in comments.

Comments

0

generally i used to modify as below.

testdf = pd.read_csv("sam.csv")

testdf

   ExportIndex AgentName         Country
0            1    Prince  United Kingdom
1            2       Nit  United Kingdom
2            3     Akhil  United Kingdom
3            4     Ruiva  United Kingdom
4            5     Niraj  United Kingdom
5            6     Nitin   United States

i = 0
while(i != len(testdf)):
    if(testdf['AgentName'][i] == 'Nit'):
        testdf['AgentName'][i] = 'Nitesh'
    i += 1

testdf

   ExportIndex AgentName         Country
0            1    Prince  United Kingdom
1            2    Nitesh  United Kingdom
2            3     Akhil  United Kingdom
3            4     Ruiva  United Kingdom
4            5     Niraj  United Kingdom
5            6     Nitin   United States

But i'm not sure what's wrong with your approach.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.