Pandas and list usage in Python

linkaiyi
Follow

Reverse the python list


s= [1, 2, 3, 4, 5]
s[-3:-1] # yield [3, 4] but s[-1:-3] yields nothing
s[-1:] # 5
s[:-3] # [1, 2]

Pandas add new row to existing dataframe

1. use loc

row = [iname, ipassword, iemail]
df.loc[len(df)] = row # iloc can not modify df, but loc can
df.to_csv("login.csv", index=False)

2. use append

iname = "name1"    
ipassword = "password1"
iemail = "email@domain.com"

df2 = df.append(pd.DataFrame([[iname,ipassword,iemail]], columns
=df.columns)) # important to use [[]] add new row, otherwise will add a column
df2.to_csv("login.csv", index=False)

Index in dataframe and serie

In order to locate the row, we can use loc or iloc, using pd.loc[index] and pd.iloc[sequence].

https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

Object has 0 attachments

Was this article helpful?

This article is viewed 20 times!

0 Comments

Leave a Comment

Support