In [1]:
# Import libraries
import pandas as pd
import sys

In [2]:
# Our small data set
list_for_data = [0,1,2,3,4,5,6,7,8,9]

# Create dataframe
df = pd.DataFrame(list_for_data)
df


Out[2]:
0
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

In [3]:
df.columns


Out[3]:
Int64Index([0], dtype='int64')

In [4]:
df.columns = ['Rev']
df


Out[4]:
Rev
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

In [5]:
# Adding new column
df['NewCol'] = 5

In [6]:
df


Out[6]:
Rev NewCol
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

In [7]:
# Lets modify our new column
df['NewCol'] = df['NewCol'] + 1
df


Out[7]:
Rev NewCol
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 [8]:
# We can delete columns
del df['NewCol']
df


Out[8]:
Rev
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9

In [9]:
# Lets add a couple of columns
df['test'] = 3
df['col'] = df['Rev']
df


Out[9]:
Rev test 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

In [10]:
df.index


Out[10]:
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')

In [11]:
# change the name of the index
i = ['a','b','c','d','e','f','g','h','i','j']
df.index = i
df


Out[11]:
Rev test 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

In [12]:
# select pieces of the dataframe using loc.
df.loc['a']


Out[12]:
Rev     0
test    3
col     0
Name: a, dtype: int64

In [13]:
# df.loc[inclusive:inclusive] -> few rows
df.loc['a':'d']


Out[13]:
Rev test col
a 0 3 0
b 1 3 1
c 2 3 2
d 3 3 3

In [14]:
# first three rows
df.iloc[0:3]


Out[14]:
Rev test col
a 0 3 0
b 1 3 1
c 2 3 2

In [15]:
df[['Rev', 'test']]


Out[15]:
Rev test
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 [16]:
df['col'][5:]


Out[16]:
f    5
g    6
h    7
i    8
j    9
Name: col, dtype: int64

In [17]:
# Select top N number of records (default = 5)
df.head()


Out[17]:
Rev test col
a 0 3 0
b 1 3 1
c 2 3 2
d 3 3 3
e 4 3 4

In [18]:
# Select bottom N number of records (default = 5)
df.tail()


Out[18]:
Rev test col
f 5 3 5
g 6 3 6
h 7 3 7
i 8 3 8
j 9 3 9

In [ ]: