这是我使用过的代码
iname = "name1"
ipassword = "password1"
iemail = "email@domain.com"
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)
如何使用pandas dataframe将名称,密码和电子邮件存储在csv文件中。
login.csv
name password email
admin admin asdfs
zds sd dsssfsfd
vipul rao dsfdsfs
答案 0 :(得分:4)
另一种简单的方法是使用pd.Dataframe.loc方法。
row = [iname, ipassword, iemail]
df.loc[len(df)] = row
df.to_csv("login.csv", index=False)
答案 1 :(得分:3)
使用 -
iname = "name1"
ipassword = "password1"
iemail = "email@domain.com"
df2 = df.append(pd.DataFrame([[iname,ipassword,iemail]], columns
=df.columns))
df2.to_csv("login.csv", index=False)
<强>输出强>
name password email
0 admin admin asdfs
1 zds sd dsssfsfd
2 vipul rao dsfdsfs
0 name1 password1 email@domain.com
答案 2 :(得分:1)
您可以使用pd.DataFrame.loc
向数据框添加行:
iname = "name1"
ipassword = "password1"
iemail = "email@domain.com"
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)
答案 3 :(得分:0)