-
Notifications
You must be signed in to change notification settings - Fork 65
/
Series_vs_DataFrame.py
37 lines (31 loc) · 1.85 KB
/
Series_vs_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
# -*- coding: utf-8 -*-
__author__ = 'RicardoMoya'
import pandas as pd
# Series: Is a unidimensional array with indexation. Similar to dictionary
spanishPlayers = pd.Series(
['Casillas', 'Ramos', 'Pique', 'Puyol', 'Capdevila', 'Xabi Alonso', 'Busquets', 'Xavi Hernandez', 'Pedrito',
'Iniesta', 'Villa'], index=[1, 15, 3, 5, 11, 14, 16, 8, 18, 6, 7])
print "Spanish Football Players: \n%s" % spanishPlayers
print '\n==================================================================\n'
# series through dictionary
dictPlayers = {1: 'Casillas', 15: 'Ramos', 3: 'Pique', 5: 'Puyol', 11: 'Capdevila', 14: 'Xabi Alonso',
16: 'Busquets', 8: 'Xavi Hernandez', 18: 'Pedrito', 6: 'Iniesta', 7: 'Villa'}
players2series = pd.Series(dictPlayers)
# Insert new player
players2series[10] = 'Cesc'
print "Spanish Football Players through dictionary: \n%s" % players2series
print '\n==================================================================\n'
# DataFrame: data structure similar to excel or SQL table
spanishPlayersDF = pd.DataFrame(
{
'name': ['Casillas', 'Ramos', 'Pique', 'Puyol', 'Capdevila', 'Xabi Alonso', 'Busquets', 'Xavi Hernandez',
'Pedrito', 'Iniesta', 'Villa'],
'demarcation': ['Goalkeeper', 'Right back', 'Centre-back', 'Centre-back', 'Left back', 'Defensive midfield',
'Defensive midfield', 'Midfielder', 'Left winger', 'Right winger', 'Centre forward'],
'team': ['Real Madrid', 'Real Madrid', 'FC Barcelona', 'FC Barcelona', 'Villareal', 'Real Madrid',
'FC Barcelona', 'FC Barcelona', 'FC Barcelona', 'FC Barcelona', 'FC Barcelona']
}, columns=['name', 'demarcation', 'team'], index=[1, 15, 3, 5, 11, 14, 16, 8, 18, 6, 7]
)
# Insert new player
spanishPlayersDF.loc[10] = ['Cesc', 'Forward', 'Arsenal']
print "Spanish Football Players DataFrame: \n%s" % spanishPlayersDF