-
Notifications
You must be signed in to change notification settings - Fork 3
/
02_01_01_pandas_columns.py
97 lines (57 loc) · 1.6 KB
/
02_01_01_pandas_columns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
#importacion de pandas
import pandas as pd
import sys
print('Python version ' + sys.version)
print('Pandas version: ' + pd.__version__)
# Our small data set
d = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Create dataframe
df = pd.DataFrame(d)
print(df)
print(df.shape)
# Lets change the name of the column
df.columns = ['Rev']
print(df)
# Lets add a column
df['NewCol'] = 5
print(df)
# Lets modify our new column
df['NewCol'] = df['NewCol'] + 1
print(df)
# Lets add a new column with column data
df['NewCol2'] = df['NewCol'] + 1
print(df)
# We can delete column
del df['NewCol2']
print(df)
# Lets add a couple of columns
df['test'] = 3
df['col'] = df['Rev'] + 1
print(df)
# If we wanted, we could change the name of the index
i = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df.index = i
print(df)
print(df.loc['a'])
# df.loc[inclusive:inclusive]
print(df.loc['a':'d'])
# df.iloc[inclusive:exclusive]
# Note: .iloc is strictly integer position based. It is available from [version 0.11.0] (http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#v0-11-0-april-22-2013)
print(df.iloc[0:3])
print(df['Rev'])
print(df[['Rev', 'test']])
# df.ix[rows,columns]
# replaces the deprecated ix function
#df.ix[0:3,'Rev']
print(df.loc[df.index[0:3],'Rev'])
# replaces the deprecated ix function
#df.ix[5:,'col']
print(df.loc[df.index[5:],'col'])
# replaces the deprecated ix function
#df.ix[:3,['col', 'test']]
print(df.loc[df.index[:3],['col', 'test']])
# Select top N number of records (default = 5)
print(df.head())
# Select bottom N number of records (default = 5)
print(df.tail())