In [25]:
import pandas as pd
In [26]:
person = {
    "first": ["Kushagra", "Jane", "John"],
    "last": ["Gupta", "Doe", "Doe"],
    "email": ["Kushagra225@gmail.com", "JaneDoe123@gmail.com", "John@gmail.com"]
}
In [27]:
df = pd.DataFrame(person)
In [28]:
df
Out[28]:
first last email
0 Kushagra Gupta Kushagra225@gmail.com
1 Jane Doe JaneDoe123@gmail.com
2 John Doe John@gmail.com
In [29]:
#Adding Column
df['full_name'] = df['first'] + ' ' + df['last']
df
Out[29]:
first last email full_name
0 Kushagra Gupta Kushagra225@gmail.com Kushagra Gupta
1 Jane Doe JaneDoe123@gmail.com Jane Doe
2 John Doe John@gmail.com John Doe
In [30]:
#Deleting Column
df.drop(columns=['first','last'], inplace=True)
df
Out[30]:
email full_name
0 Kushagra225@gmail.com Kushagra Gupta
1 JaneDoe123@gmail.com Jane Doe
2 John@gmail.com John Doe
In [31]:
df['full_name'].str.split(' ', expand=True)
Out[31]:
0 1
0 Kushagra Gupta
1 Jane Doe
2 John Doe
In [32]:
#creating first and last column again
df[['first', 'last']] = df['full_name'].str.split(' ', expand=True)
df
Out[32]:
email full_name first last
0 Kushagra225@gmail.com Kushagra Gupta Kushagra Gupta
1 JaneDoe123@gmail.com Jane Doe Jane Doe
2 John@gmail.com John Doe John Doe
In [34]:
#adding row in df
df.append({'first': 'Tony'}, ignore_index=True)
Out[34]:
email full_name first last
0 Kushagra225@gmail.com Kushagra Gupta Kushagra Gupta
1 JaneDoe123@gmail.com Jane Doe Jane Doe
2 John@gmail.com John Doe John Doe
3 NaN NaN Tony NaN
In [35]:
person = {
    "first": ["Tony", "Steve"],
    "last": ["Stark", "Rogers"],
    "email": ["IronMan@avenge.com", "CaptainAmerica@avenge.com"]
}
df2= pd.DataFrame(person)
In [36]:
df2
Out[36]:
first last email
0 Tony Stark IronMan@avenge.com
1 Steve Rogers CaptainAmerica@avenge.com
In [37]:
#Appending df to another df
df.append(df2, ignore_index=True)
/Users/kushagra/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py:7123: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.

To accept the future behavior, pass 'sort=False'.

To retain the current behavior and silence the warning, pass 'sort=True'.

  sort=sort,
Out[37]:
email first full_name last
0 Kushagra225@gmail.com Kushagra Kushagra Gupta Gupta
1 JaneDoe123@gmail.com Jane Jane Doe Doe
2 John@gmail.com John John Doe Doe
3 IronMan@avenge.com Tony NaN Stark
4 CaptainAmerica@avenge.com Steve NaN Rogers
In [39]:
#sort=True is used for suppressing the warning
df = df.append(df2, ignore_index=True, sort=False)
In [40]:
df
Out[40]:
email full_name first last
0 Kushagra225@gmail.com Kushagra Gupta Kushagra Gupta
1 JaneDoe123@gmail.com Jane Doe Jane Doe
2 John@gmail.com John Doe John Doe
3 IronMan@avenge.com NaN Tony Stark
4 CaptainAmerica@avenge.com NaN Steve Rogers
In [41]:
#deleting row/s
df.drop(index=4)
Out[41]:
email full_name first last
0 Kushagra225@gmail.com Kushagra Gupta Kushagra Gupta
1 JaneDoe123@gmail.com Jane Doe Jane Doe
2 John@gmail.com John Doe John Doe
3 IronMan@avenge.com NaN Tony Stark
In [46]:
df[df['last'] == 'Doe']
Out[46]:
email full_name first last
1 JaneDoe123@gmail.com Jane Doe Jane Doe
2 John@gmail.com John Doe John Doe
In [48]:
#deleting rows with last name 'Doe'
filt = df['last'] == 'Doe'
#getting indexes of rows
df.drop(index=df[filt].index)
Out[48]:
email full_name first last
0 Kushagra225@gmail.com Kushagra Gupta Kushagra Gupta
3 IronMan@avenge.com NaN Tony Stark
4 CaptainAmerica@avenge.com NaN Steve Rogers