Pandas Adding - Deleting columns and Index Operations

Author: Al-mamun Sarkar Date: 2020-04-01 16:59:51

Pandas Adding - Deleting columns and Index Operations. The following shows how to add column, delete column from pandas DataFrame and various index Operations using pandas module.

 

In [1]:

import pandas as pd

 

In [2]:

data_list = [0,1,2,3,4,5,6,7,8,9]

data_frame = pd.DataFrame(data_list)
data_frame

Out[2]:

  0
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

 

 

Change the name of the column:

In [3]:

data_frame.columns = ['rev']
data_frame

Out[3]:

  rev
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

 

Add a column:

In [4]:

data_frame['new_col'] = 5
data_frame

Out[4]:

  rev new_col
0 0 5
1 1 5
2 2 5
3 3 5
4 4 5
5 5 5
6 6 5
7 7 5
8 8 5
9 9 5

 

Modify our new column:

In [5]:

data_frame['new_col'] = data_frame['new_col'] + 1
data_frame

Out[5]:

  rev new_col
0 0 6
1 1 6
2 2 6
3 3 6
4 4 6
5 5 6
6 6 6
7 7 6
8 8 6
9 9 6

 

In [6]:

del data_frame['new_col']
data_frame

Out[6]:

  rev
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

 

Adding couple of columns:

In [7]:

data_frame['test_col'] = 3
data_frame['another_col'] = data_frame['rev']
data_frame

Out[7]:

  rev test_col another_col
0 0 3 0
1 1 3 1
2 2 3 2
3 3 3 3
4 4 3 4
5 5 3 5
6 6 3 6
7 7 3 7
8 8 3 8
9 9 3 9

 

Change the name of the index:

In [8]:

index = ['a','b','c','d','e','f','g','h','i','j']
data_frame.index = index
data_frame

Out[8]:

  rev test_col another_col
a 0 3 0
b 1 3 1
c 2 3 2
d 3 3 3
e 4 3 4
f 5 3 5
g 6 3 6
h 7 3 7
i 8 3 8
j 9 3 9

 

Select pieces of the dataframe using loc:

In [9]:

data_frame.loc['a']

Out[9]:

rev            0
test_col       3
another_col    0
Name: a, dtype: int64

 

In [10]:

data_frame.loc['a':'d']

Out[10]:

  rev test_col another_col
a 0 3 0
b 1 3 1
c 2 3 2
d 3 3 3

 

In [11]:

data_frame.iloc[0:4]

Out[11]:

  rev test_col another_col
a 0 3 0
b 1 3 1
c 2 3 2
d 3 3 3

 

Select using the column name:

In [12]:

data_frame['rev']

Out[12]:

a    0
b    1
c    2
d    3
e    4
f    5
g    6
h    7
i    8
j    9
Name: rev, dtype: int64

 

In [13]:

data_frame[['rev', 'test_col']]

Out[13]:

  rev test_col
a 0 3
b 1 3
c 2 3
d 3 3
e 4 3
f 5 3
g 6 3
h 7 3
i 8 3
j 9 3

 

In [14]:

data_frame.loc[data_frame.index[0:3], 'rev']

Out[14]:

a    0
b    1
c    2
Name: rev, dtype: int64

 

In [15]:

data_frame.loc[data_frame.index[0:3], ['rev', 'test_col']]

Out[15]:

  rev test_col
a 0 3
b 1 3
c 2 3

 

In [16]:

data_frame.loc[data_frame.index[5:],'another_col']

Out[16]:

f    5
g    6
h    7
i    8
j    9
Name: another_col, dtype: int64

 

In [17]:

data_frame.loc[data_frame.index[:3],['another_col', 'test_col']]

Out[17]:

  another_col test_col
a 0 3
b 1 3
c 2 3

 

Select top N number of records:

In [18]:

n = 5
data_frame.head(n)

Out[18]:

  rev test_col another_col
a 0 3 0
b 1 3 1
c 2 3 2
d 3 3 3
e 4 3 4

 

Select bottom N number of records:

In [19]:

n = 5
data_frame.tail(5)

Out[19]:

  rev test_col another_col
f 5 3 5
g 6 3 6
h 7 3 7
i 8 3 8
j 9 3 9