Skip to content

Commit 998fdae

Browse files
authored
Add files via upload
1 parent 3651a89 commit 998fdae

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

Python Pandas - Series.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
2+
# coding: utf-8
3+
4+
# ### Create Series
5+
6+
# In[2]:
7+
8+
9+
# Create an empty series
10+
11+
import pandas as pd
12+
s= pd.Series()
13+
print(s)
14+
15+
16+
# In[4]:
17+
18+
19+
# Example 1 : Create a series from an ndarray
20+
21+
import pandas as pd
22+
import numpy as np
23+
24+
# Array is created from a list
25+
data=np.array(['a','b','c','d'])
26+
27+
# A series is created from the array with the default index
28+
s=pd.Series(data)
29+
print (s)
30+
31+
32+
# In[5]:
33+
34+
35+
# Example 2 : Create a series from an ndarray
36+
37+
import pandas as pd
38+
import numpy as np
39+
40+
# Array is created from a list
41+
data=np.array(['a','b','c','d'])
42+
43+
# A series is created from the array with the default index
44+
s=pd.Series(data,index=[100,101,102,103])
45+
print (s)
46+
47+
48+
# ### Create a series from a dictionary
49+
50+
# In[8]:
51+
52+
53+
# Create a series from a dictionary
54+
55+
import pandas as pd
56+
import numpy as np
57+
58+
# Declare a dictionary wuth keys 'a','b','c'
59+
aDict={'a':0.,'b':1.,'c':2.}
60+
61+
# Create a series from this dictionary
62+
s=pd.Series(aDict)
63+
print (s)
64+
65+
66+
# In[10]:
67+
68+
69+
# Create a series from a dictionary
70+
71+
import pandas as pd
72+
import numpy as np
73+
74+
# Declare a dictionary wuth keys 'a','b','c'
75+
data={'a':0.,'b':1.,'c':2.}
76+
77+
# Create a series from this dictionary with specific indices
78+
# The dict has only three items
79+
s=pd.Series(data,index=['b','c','d','a'])
80+
print (s)
81+
82+
83+
# ### Create a series from scalar values
84+
85+
# In[12]:
86+
87+
88+
# Create a series from scalar values
89+
90+
import pandas as pd
91+
import numpy as np
92+
93+
# Create a series
94+
s=pd.Series(5,index=[0,1,2,3])
95+
print (s)
96+
97+
98+
# ### Accessing Data from Series with Position
99+
#
100+
#
101+
102+
# In[13]:
103+
104+
105+
# Data in the series can be accessed similar to that in ndarray
106+
107+
import pandas as pd
108+
s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
109+
110+
#retrieve the first element
111+
print(s[0])
112+
113+
114+
# In[14]:
115+
116+
117+
import pandas as pd
118+
s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
119+
120+
#retrieve the first 3 elements from 0 -3 not including 3
121+
# retrieve 0,1,2
122+
print(s[:3])
123+
124+
print('\n')
125+
126+
#retrieve the last 3 elements
127+
print(s[-3:])
128+
129+
print('\n')
130+
131+
#retrieve a single element at a specific index
132+
print(s['a'])
133+
print('\n')
134+
135+
#retrieve multiple elements using a list of index labels
136+
print(s[['a','c','d']])
137+

0 commit comments

Comments
 (0)