2

I have a pandas data frame with 2 columns(UserId,RandNo). In this UserId has the values for 10 rows as below

enter image description here

Now, I fill the RandNo column through for loop as below.

import pandas as pd
import random

df=pd.read_csv('df_sto.csv', skipinitialspace=True)

rand=0
for index, row in df.iterrows():
    try:
            rand=random.randint(0,100)

            df.at[index, 'RandNo'] = rand           
    except Exception as e:
        print(e)
df.to_csv("df_sto1.csv", sep=',')

Here, I get the updated value in df_sto1.csv file only, the updated value not affected in df_sto.csv.

If the data frame rows are large and if the for loop face any problem after 95% of rows are updated in df.iterrows(): for 'df_sto.csv' Then I want to repeat the process form 0% itself(from 0th row itself). To avoid this problem I want to update the data frame for 'df_sto.csv' each and every for loop iteration itself rather than to get updated value through df.to_csv("df_sto1.csv", sep=',')

Guide me to update the data frame cell value using for loop in each iteration itself. Thanks in advance.

2
  • Can anybody suggest me the solution for this this situation. Thanks Commented Mar 15, 2019 at 3:30
  • Is there any reason you don't want to update/fill the RandNo column at once? Commented Mar 15, 2019 at 4:21

1 Answer 1

1

I don't think the try block is necessary at all but if you insist on using it, perhaps the operations are more than just assigning random numbers, then how about wrapping it up in a function?

import pandas as pd
import random

df=pd.read_csv('df_sto.csv', skipinitialspace=True)
copy_df = df.copy()

def update_df(frame):
    for index, row in frame.iterrows():
        rand=random.randint(0,100)
        frame.at[index, 'RandNo'] = rand

    return frame


status = 0
while status == 0:
    try:
        copy_df = update_df(copy_df)
        status = 1
    except Exception as e:
        copy_df = df
        print(e)


df = copy_df
df.to_csv("df_sto1.csv", sep=',')
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks kerwei for your response. Using your answer I comment the last line"df.to_csv("df_sto1.csv", sep=',')" then I execute. The code not affect the "df_sto.csv". Also in my question I mentioned that for loop face any problem at 95% or in middle of the process, I need to restart from first. To overcome this situation is my problem. But as per your code it is not resolved. My request is to update the opened "df_sto.csv" not in new *.csv file. I request you to reconsider your answer to resolve my problem. Note: try block is not mandatory for my code.
Okay, I think maybe I didn't catch your question properly. Are you trying to 1. read the contents of df_sto.csv, 2. Add in the column with random numbers and 3. Save the dataframe with the new column back into df_sto.csv?
Yes. 1. I read the contents of df_sto.csv, 2. The column UserId has 10 values(rows) but corresponding RandNo column has empty values 3. Now I want to update the RandNo column with random values corresponding to userId column rows in the same df_sto.csv file. Note: Here, I used random number is for your convenient only. Originally I want to update different column name and values for my problem. Also my for loop executes slow manner and that time after part of execution for loop face some problem and then successful executed value is not updated in the df_sto.csv file.
So I want to restart the for loop from 0 state. This is my problem. Guide me to overcome this scenario. Thanks.
@user1999109 I think I get what you mean. Creating a copy before starting the loop should help. Let me update my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.