0% found this document useful (0 votes)
7 views

Python Pandas (II)

The document discusses the Pandas Series data structure. A Series can be created from many different data types including lists, dicts, and scalars. Data and index values can be specified. Individual values in a Series can be accessed by index or label.

Uploaded by

tnandyala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Pandas (II)

The document discusses the Pandas Series data structure. A Series can be created from many different data types including lists, dicts, and scalars. Data and index values can be specified. Individual values in a Series can be accessed by index or label.

Uploaded by

tnandyala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Python Pandas(II)

Series Data Structure


 The basic method to create a Series is to call:

 import pandas as <identifier name>


 <series name>=<identifier name> .Series(data, index,
dtype, copy)

Data can be many different things:

 a Python dict
 a Python list
 a Python tuple

The passed index is a list of axis labels.


Sr.No Parameter & Description

1 data
data takes various forms like ndarray, list, constants

2 index
Index values must be unique and hashable, same length
as data.

3 dtype
dtype is for data type. If None, data type will be inferred

4 copy
Copy data. Default False
Create an Empty Series

 A basic series, which can be created is an Empty Series.

 Example

#import the pandas library and aliasing as pd


import pandas as pd
s = pd.Series()
print s

 Its output is as follows −


Series([], dtype: float64)
Create Series Without index
e.g.

import pandas as pd1


import numpy as np1
data = np1.array(['a','b','c','d'])
s = pd1.Series(data)
print(s)

Output
0a
1b
2c
3 d dtype: object

Note : default index is starting from 0


With index position
e.g.
import pandas as pd1
import numpy as np1
data = np1.array(['a','b','c','d'])
s = pd1.Series(data , index=[100,101,102,103])
print(s)
Output
100 a
101 b
102 c
103d dtype: object
Note : index is starting from 100
Create a Series from dictionary
 A dict can be passed as input and if no index is specified, then the
dictionary keys are taken in a sorted order to construct index.
If index is passed, the values in data corresponding to the labels in
the index will be pulled out.
Eg.1(without index)
import pandas as pd1
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd1.Series(data)
print(s)

 Output

 a 0.0
 b 1.0
 c 2.0 dtype: float64
Eg.2 (with index)
import pandas as pd1
import numpy as np1
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd1.Series(data,index=['b','c','d','a'])
print(s)

Output
b 1.0
c 2.0
d NaN
a 0.0 dtype: float64

 Note − Index order is persisted and the missing element is filled with NaN (Not a Number).
Create a Series from Scalar
 If data is a scalar value, an index must be provided. The
value will be repeated to match the length of index
Eg-3
import pandas as pd1
import numpy as np1
s = pd1.Series(5, index=[0, 1, 2, 3])
print(s)

Output
0 5
1 5
2 5
3 5
Accessing Data from Series with Position

 Data in the series can be accessed similar to that in


an ndarray.
Example 1

import pandas as pd
s = pd.Series([1,2,3,4,5],index =['a','b','c','d','e'])
#retrieve the first element
print (s[0])
Print( s[‘c’])

 Its output is as follows −

 1
 3
Accessing Data from Series
with indexing and slicing
Example 2

import pandas as pd1


s = pd1.Series([1,2,3,4,5],index =['a','b','c','d','e'])
print (s[0])
# for 0 index position
print (s[ :3])
#for first 3 index values
print (s[-3: ])
# slicing for last 3 index values
Output

1
a. 1
b. 2
c. 3
dtype: int64
c3
d. 4
e. 5
dtype: int64
How to Read Text Files with Pandas?
 We can read the text files by Using read_csv()

 import pandas as pd
 # read text file into pandas DataFrame
 df = pd.read_csv("gfg.txt", sep=" ")
 print(df)

You might also like