-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlesson3-dataframe.py
53 lines (40 loc) · 1.14 KB
/
lesson3-dataframe.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 25 08:48:03 2018
@author: lpa2a
"""
# import convention
from pandas import Series, DataFrame
import pandas as pd
import numpy as np
### creating a data frame
# dict of equal-length lists or NumPy arrays
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}
df = DataFrame(data)
print(df)
# specify columns by name to pick order
print(DataFrame(data, columns=['year', 's', 'p']))
# retrieving a column
print(df.year)
# or
print(df['year'])
###
#### The column returned when indexing a DataFrame is a view on the underlying data, not a copy.
###
# notice that a column of a dataframe is a series
print(type(df.year))
# retrieving a row
print(df.loc[0]) # label based
print(df.ix[0]) # depricated
print(df.iloc[0]) # poistional based
# create a new column - act like it exists - like a python dictionary
print(df)
df['votes']=2
print(df)
# another way to make a data frame
pop = {'Nevada': {2001: 2.4, 2002: 2.9},'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
df3 = DataFrame(pop)
print(df3)