32

This is the code I have used:

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)

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
vvvvv
  • 25,404
  • 19
  • 49
  • 81
vipul-rao
  • 387
  • 1
  • 5
  • 12

5 Answers5

61

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)
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • 4
    Hi. That is a neat answer indeed. I was just curious to know why doesn't ```df.iloc[len(df)]=row``` work in this case? – badavadapav Aug 10 '20 at 13:09
  • 4
    @RaghavKumar Design decision, `iloc cannot enlarge its target object`, but `loc` can – smcs Dec 04 '20 at 11:02
  • @smcs What does enlarging the target object mean? – Connor Dec 12 '21 at 16:10
  • 1
    @Connor The target object is the data frame, and after adding a new row, it is larger than before, depending on the number of columns. – smcs Dec 13 '21 at 09:42
  • 1
    @smcs Thanks so much! What is the efficiency of something like that? Is is quicker to use loc to enlarge the array or make something like a dictionary first? – Connor Dec 13 '21 at 14:01
9

Use -

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)

Output

    name   password             email
0  admin      admin             asdfs
1    zds         sd          dsssfsfd
2  vipul        rao           dsfdsfs
0  name1  password1  email@domain.com
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
3

You can use pd.DataFrame.loc to add a row to your dataframe:

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)
jpp
  • 159,742
  • 34
  • 281
  • 339
2

A good way is to create an empty list first, populate it and then add to the empty data frame like this

data=[]

for i, row in new_df.head(4).iterrows():
    sequence=str(row['body'])
    author=row['author']
    data.append([author,sequence]) 
d=pd.DataFrame(data,columns = ['author', 'results'])

it will give the results like this enter image description here

Shaina Raza
  • 1,474
  • 17
  • 12
2

The accepted answer is good if all you are doing is appending rows. However, if you do other operations such as:

df.drop_duplicates(subset=['name'],inplace=True)

then some of the index values will be greater than the size of the dataframe, and the accepted answer may overwrite an existing row.

In that case, I recommend:

row = [iname, ipassword, iemail]
df.loc[max(df.index)+1] = row
df.to_csv("login.csv", index=False)

which, if the dataframe could be empty, may have to become:

row = [iname, ipassword, iemail]
if len(df.index)>0:
    df.loc[max(df.index)+1] = row
else:
    df.loc[len(df)] = row
df.to_csv("login.csv", index=False)
  • That should be the correct accepted answer. I have encountered same problem when I dropped a row and then tried to append a new one at the end with `df.loc[len(df)] = ...` and the last existing row got overwritten – user164863 Jul 27 '23 at 12:19