forked from le-campus-numerique/GIT_pandas-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Series_vs_DataFrame.py
40 lines (33 loc) · 1.93 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
38
39
40
# -*- coding: utf-8 -*-
__author__ = 'Derrick'
__author__ = 'Ed'#Hello ED
__author__='Nadia'
__author__='Timothée' #Name of last author
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', 'Raul'], index=[1, 15, 3, 5, 11, 14, 16, 8, 18, 6, 7, 7])
print("Spanish Football Players: \n%s" % spanishPlayers)
# 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', 7: 'Raul'}
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', 'Raul'],
'demarcation': ['Goalkeeper', 'Right back', 'Centre-back', 'Centre-back', 'Left back', 'Defensive midfield',
'Defensive midfield', 'Midfielder', 'Left winger', 'Right winger', 'Centre forward', 'Centre forward'],
'team': ['Real Madrid', 'Real Madrid', 'FC Barcelona', 'FC Barcelona', 'Villareal', 'Real Madrid',
'FC Barcelona', 'FC Barcelona', 'FC Barcelona', 'FC Barcelona', 'FC Barcelona', 'Real Madrid']
}, columns=['name', 'demarcation', 'team'], index=[1, 15, 3, 5, 11, 14, 16, 8, 18, 6, 7, 7]
)
# Insert new player
spanishPlayersDF.loc[10] = ['Cesc', 'Forward', 'Arsenal']
print("Spanish Football Players DataFrame: \n%s" % spanishPlayersDF)