change a value in a csv file using pandas python without knowing its index
Question:
I need to change a value in csv file without knowing its index
but by using another value in the same row
df = pd.read_csv("dataframe.csv")
result = df[df['USERNAME'] == login.user_name]
result = result['CARD_BALANCE'].values[0]
if result>=20:
a=result+100
df.loc[df['USERNAME'] == result, "CARD_BALANCE"] = a
df.to_csv('dataframe.csv', index=False)
THIS IS NOT WORKING. HELP ME
Answers:
Your code is almost work, you just change one line from
df.loc[df['USERNAME'] == result, "CARD_BALANCE"] = a
become
df.loc[df['USERNAME'] == login.user_name, "CARD_BALANCE"] = a
I need to change a value in csv file without knowing its index
but by using another value in the same row
df = pd.read_csv("dataframe.csv")
result = df[df['USERNAME'] == login.user_name]
result = result['CARD_BALANCE'].values[0]
if result>=20:
a=result+100
df.loc[df['USERNAME'] == result, "CARD_BALANCE"] = a
df.to_csv('dataframe.csv', index=False)
THIS IS NOT WORKING. HELP ME
Your code is almost work, you just change one line from
df.loc[df['USERNAME'] == result, "CARD_BALANCE"] = a
become
df.loc[df['USERNAME'] == login.user_name, "CARD_BALANCE"] = a