Hello everyone. Welcome to the Pandas series Lecture where we are going to introduce the series, how it works with several examples.
Lets go ahead…
What is Pandas Series?
Pandas Series is a one-dimensional array that is capable of holding data of all types like integer, float, boolean, etc. It is like a column in a table.
The first main data type we will learn about for pandas is the Series data type. Let’s import Pandas and explore the Series object.
A Series is very similar to a NumPy array (in fact it is built on top of the NumPy array object). What differentiates the NumPy array from a Series, is that a Series can have axis labels, meaning it can be indexed by a label, instead of just a number location. It also doesn’t need to hold numeric data, it can hold any arbitrary Python object.
Let’s explore this concept through some examples:
Creating a Series
You can convert a list,numpy array, or dictionary to a Series
import numpy as np
import pandas as pd
labels = ['a','b','c']
my_list = [10,20,30]
arr = np.array([10,20,30])
d = {'a':10,'b':20,'c':30}
**using list**
pd.Series(data=my_list)
#using labels
pd.Series(data=my_list,index=labels)
Output:
you can also write labels without writing index like:
Data in a Series
As we know from the above paragraph that a Series can hold a variety of object types:
pd.Series(data=labels)
Output:
# Even functions (although unlikely that you will use this)
pd.Series([sum,print,len])
Using an Index
The key to using a Series is understanding its index. Pandas makes use of these index names or numbers by allowing for fast look ups of information (works like a hash table or dictionary).
Let’s see some examples of how to grab information from a Series. Let us create two series, ser1 and ser2:
ser1 = pd.Series([1,2,3,4],index = ['USA', 'Germany','USSR', 'Japan'])
#print
ser1
ser2 = pd.Series([1,2,5,4],index = ['USA', 'Germany','Italy', 'Japan'])
#print
ser2
Output:
Operations are then also done based off of index:
ser1+ser2
Output:
FAQs:
When List_1 is concatenated with List_2 , does the length of the List_1 change?
Ans. The length of the List_1 will not change unless you update it in the existing list. If
we assign it to a new variable length of the list changes depending on the number of
elements.
How to add Pandas to spyder?
Ans-Import pandas as pd
Instead of returning the data types for all columns, can the data type of single column
be returned?
Ans-Yes, the data type of a single column can be returned using the following command.
(df1-name of the existing dataframe)
df1[‘column_name’].dtypes
How do you deal with special or junk characters in data?
Ans-By converting special or junk characters to nan, the data cleaning process is made
easy. This is because the pandas libraries offers several functions that help in handling nan
values. For instance the function pandas.DataFrame.dropna() is used to drop rows with nan
values and pandas.DataFrame.fillna() is used to fill the nan values with the given value.
How to import a .csv file?
Ans-To import .csv files, use the following command:
pandas.read_csv(“path”)
The output for the .txt file does not read “??” and “###” values, instead it is showing
the average values for the applicable column values.
Ans-Use any of the following codes to read the text data into Spyder:
data_txt1=pd.read_table(‘Iris_data.txt’,delimiter=” “)
data_txt2=pd.read_csv(‘Iris_data.txt’,delimiter=” “)
Clear your console and environment variables and then try reading it again.
Must read: Python Pandas Tutorial – Introduction
This is all about the Pandas series, Let’s stop here for now and move on to DataFrames, which will expand on the concept of Series!
In this article, we have done practical examples more than theoretical. If you want more documents on Pandas then you can visit the GitHub pandas site.