This is the code I have used:
iname = "name1"
ipassword = "password1"
iemail = "[email protected]"
res1 = []
df = pd.read_csv("login.csv", sep=',', encoding="utf-8")
res1.append(iname,ipassword,iemail)
print(res1,res2,res3)
df.to_csv("login.csv", index=False)
How to store the name, password and email in the csv file by using pandas
dataframe?
login.csv
:
name password email
admin admin asdfs
zds sd dsssfsfd
vipul rao dsfdsfs
Another simple approach is to use pd.Dataframe.loc method.
row = [iname, ipassword, iemail]
df.loc[len(df)] = row
df.to_csv("login.csv", index=False)
Use -
iname = "name1"
ipassword = "password1"
iemail = "[email protected]"
df2 = df.append(pd.DataFrame([[iname,ipassword,iemail]], columns
=df.columns))
df2.to_csv("login.csv", index=False)
Output
name password email
0 admin admin asdfs
1 zds sd dsssfsfd
2 vipul rao dsfdsfs
0 name1 password1 [email protected]
You can use pd.DataFrame.loc
to add a row to your dataframe:
iname = "name1"
ipassword = "password1"
iemail = "[email protected]"
df = pd.read_csv("login.csv", sep=',', encoding="utf-8")
df.loc[df.index.max()+1] = [iname, ipassword, iemail]
df.to_csv("login.csv", index=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With