L
L
LakeForest2021-10-13 15:06:46
Python
LakeForest, 2021-10-13 15:06:46

Why, after changing the rows in the column and saving to csv, is the data unloaded (saved) before the changes?

Can you please tell me why, after changing the rows in the column and saving csv, the data is unloaded (saved) before the changes (that is, as if I did not make changes to the necessary rows)?

The scheme is this:

for index, row in df.iterrows():
    if row.targer.str.startswith("0"):
    old_result=row.result
    df.loc[index, "result"] = "0"+ old_result

df.to_csv("new_df.csv", index=False)


And it is unloaded as if I did not add "0"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LakeForest, 2021-10-13
@LakeForest

the column was exported as a number. so zero is not displayed.
decision:
df = pd.read_csv("df.csv", dtype={"result": str})

D
dmshar, 2021-10-13
@dmshar

As I understand it, this is such a test for the ability to guess what and how is written in other parts of your program.
Also, are you sure that in your code the indentation in the inner loop is exactly as you showed us?

if row.targer.str.startswith("0"):
old_result=row.result
df.loc[index, "result"] = "0"+ old_result

Or is it a test of our knowledge of the language?
But let's say that this is an error, and two lines of the inner loop are still shifted relative to the condition line.
Then there are plenty of options.
1. No changes will occur in the df object if there are no rows in the original df object. Whether they are there or not, we don't know.
2. The condition is never met. Like it or not - this we also do not know. Have you checked this yourself? 3. Okay, not what you expect is unloaded. With such a carelessly and incompletely given piece of script, guess
if row.targer.str.startswith("0"):
what is wrong in your script is very difficult. But have you done the necessary debugging yourself? Have you checked, and before uploading to a file in the object itself, what data is "old" or "new"? Or are you also suggesting that we guess?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question