Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append new row to dataframe in pandas?

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
like image 955
vipul-rao Avatar asked Apr 19 '18 08:04

vipul-rao


3 Answers

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)
like image 147
Mihai Alexandru-Ionut Avatar answered Nov 15 '22 13:11

Mihai Alexandru-Ionut


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]
like image 23
Vivek Kalyanarangan Avatar answered Nov 15 '22 13:11

Vivek Kalyanarangan


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)
like image 3
jpp Avatar answered Nov 15 '22 12:11

jpp