Chat on WhatsApp

Unleashing the Power of Pandas Series: A Comprehensive Guide to Data Manipulation and Analysis

Pandas Series is a data structure which is a one-dimensional labeled array. It can store data type such as Python objects, integers, strings, floating points etc.

 · 2 min read

In this blog post, we deep dive into the Pandas Series data structure, by exploring its key features, flexibility, and real-world applications.


Whether you are new to Pandas or a seasoned practitioner, understanding series data structure, helps in unlocking plethora of possibilities for efficient data manipulation and analysis.


Panda series is a data structure which is a one-dimensional labeled array. It can store data type such as Python objects, integers, strings, floating points etc.


Series created with the syntax

pd.Series(data, index=index)


In above syntax, index represent axis labels.


If data is of length 'n', then the index must also with same length. But if index not present, index auto generated with values states from 0,... len(data)-1


#Create series by passing list of values without index param
S=pd.Series(np.random.randn(5))
print(S)


random numbers drawn from a standard normal distribution (mean 0, standard deviation 1).



#Create series by passing list of values with index param
SeriesWithIndex=pd.Series(np.random.randn(5), ['p','q','r','s','t'])
print(SseriesWithIndex)

Generate a series with index



Create Series object from dictionary


#Create Series from dictionary
MyDict={ "Name": "Jerin", "Age": "Joseph"}
SeriesFromDict=pd.Series(MyDict)
print(SeriesFromDict)



Index label is taken from dictionary keys in pandas Series object.



Create Series object from scalar values


#Series object from scalar value


SeriesFromScalar=pd.Series(5,index=['p','q','r'])
print(SeriesFromScalar)


Create series object from scaler value must pass index



#Access elements in series object
print(SeriesFromScalar['p'])

#list multiple items
print(type(SeriesFromScalar[['p','q']]))

#list values greater than median in series
print(SeriesWithIndex[SeriesWithIndex > SeriesWithIndex.median()])


Series object items can be accessed same as ndarray. When split the series object, index also get split.



# To name the series object
SeriesWithIndex.name="Series with Index"
#SeriesWithIndex=pd.Series(np.random.randn(5), ['p','q','r','s','t'], name="Serials with Index)
print(SeriesWithIndex)


Naming of the pandas series object is done with name attribute.


Pandas series object used as array as well.


#To make pandas series as array
print(SeriesWithIndex.array)


Series is backed by ExtensionArray, but it is not NumPy ndarray.


To get NumPy ndarray, use Series.to_numpy()


#Make series object as actual nparray
print(SeriesWithIndex.to_numpy())


Set or get series object


#set or get values with index label
print(SeriesWithIndex['p'])

#check label exist in series
"p" in SeriesWithIndex
True



When use get method to access label that not exist,   NaN is returned.


print(SeriesWithIndex.get("a",np.nan))

#key error exception when access non existing label
print(SeriesWithIndex["a"])


In conclusion, pandas library and one of its core data structures like Pandas Series is one-dimensional labeled array that can hold any data type, such as integers, floats, strings, or even complex objects. Pandas Series provides powerful tools for data manipulation tasks such as filtering and transforming data.




No comments yet.

Add a comment
Ctrl+Enter to add comment