SELECTION, ADDITION, DELETION
Selection
import pandas as pd
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
df ['one']
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
Addition
Adding a new column by passing as Series
df['three']=pd.Series([10,20,30],index=['a','b','c'])
df
one | two | three | |
---|---|---|---|
a | 1.0 | 1 | 10.0 |
b | 2.0 | 2 | 20.0 |
c | 3.0 | 3 | 30.0 |
d | NaN | 4 | NaN |
Adding a new column using the existing columns in DataFrame
df['four']=df['one']+df['three']
df
one | two | three | four | |
---|---|---|---|---|
a | 1.0 | 1 | 10.0 | 11.0 |
b | 2.0 | 2 | 20.0 | 22.0 |
c | 3.0 | 3 | 30.0 | 33.0 |
d | NaN | 4 | NaN | NaN |
Deletion
using del function
del : del removes the item at a specific index. lets say del list[index]
del df['one']
df
two | three | four | |
---|---|---|---|
a | 1 | 10.0 | 11.0 |
b | 2 | 20.0 | 22.0 |
c | 3 | 30.0 | 33.0 |
d | 4 | NaN | NaN |
using pop function
pop removes the item at a specific index and returns it. lets say list.pop(index)
df.pop('two')
a 1
b 2
c 3
d 4
Name: two, dtype: int64
df
three | four | |
---|---|---|
a | 10.0 | 11.0 |
b | 20.0 | 22.0 |
c | 30.0 | 33.0 |
d | NaN | NaN |