. Advertisement .
..3..
. Advertisement .
..4..
You will need to use the method squeeze()
to convert Pandas DataFrame to a Series. Read on to find out how to use it and some examples illustrating common applications of this method.
How To Convert Pandas DataFrame To A Series
DataFrame.squeeze()
Syntax: DataFrame.squeeze(axis)
The only parameter (axis) is optional. It determines the axis you want to squeeze. There is no default value, but you can set it to 0 (equivalent to ‘index’) or 1 (‘columns’).
This method turns a one-dimensional object in Pandas, such as DataFrames or Series, into scalars. Normally, it doesn’t change the type of the object. But when given a row or column of a DataFrame, the method returns a Pandas Series.
The method squeeze()
is a great option when you have no idea whether your object is a DataFrame or a Series, except for the fact that it has just one column. Regardless, the method results in a Series.
Examples
We are going to use a DataFrame containing information about some websites.
import pandas as pd
headers = ['Site', 'Ranking', 'Type', 'Top Language']
data = [
['ITTutoria', 1, 'Tutorials', 'Python'],
['Stack Overflow', 2, 'Q&A', 'JavaScript'],
['Quora', 3, 'Q&A', None],
['Reddit', 4, 'Forum', None]
]
df = pd.DataFrame(data, columns = headers)
df
Output:
Site Ranking Type Top Language
0 ITTutoria 1 Tutorials Python
1 Stack Overflow 2 Q&A JavaScript
2 Quora 3 Q&A None
3 Reddit 4 Forum None
You might try to use the method squeeze()
for the whole DataFrame instance and be surprised by the result. The method would return the same DataFrame, not Series like you might expect.
>>> type(df.squeeze())
<class 'pandas.core.frame.DataFrame'>
This is normal behavior since the main purpose of the method squeeze()
is to convert objects into scalars. Unless your dataset has only one row or column, the resulting object is still a DataFrame.
To convert a DataFrame into Series, you will need to provide squeeze()
with a single row or column of the DataFrame. This means you will need to make use of indexing methods to specify that row or column.
For instance, use this statement if you want to convert the column labeled ‘Site’ into a Series:
>>> df['Site'].squeeze()
0 ITTutoria
1 Stack Overflow
2 Quora
3 Reddit
Name: Site, dtype: object
You can verify the data type with the function type():
>>> type(df['Site'].squeeze())
<class 'pandas.core.series.Series'>
Other column selection methods, such as the property loc, can also be used. They will return the same result:
df.Site.squeeze()
df.loc[:, 'Site'].squeeze()
df.iloc[:, 0].squeeze()
Output:
0 ITTutoria
1 Stack Overflow
2 Quora
3 Reddit
Name: Site, dtype: object
If you want to select columns based on certain conditions with loc[]
, have a look at this guide.
In a similar fashion, you can also convert a row into a Series by using its location:
>>> df.iloc[1].squeeze()
Site Stack Overflow
Ranking 2
Type Q&A
Top Language JavaScript
Name: 1, dtype: object
>>> type(df.iloc[1].squeeze())
<class 'pandas.core.series.Series'>
Note: some tutorials and old codebases use the property ix as a way to specify columns or rows in a DataFrame, such as:
>>> df.ix[:,0].squeeze()
AttributeError: 'DataFrame' object has no attribute 'ix'
The indexer .ix has been deprecated since 0.20.0. This statement may not work if you have installed a recent version of Pandas. Use their replacements (loc and iloc) instead.
Conclusion
The method squeeze()
can be used to convert Pandas DataFrame to a Series. Note that unless your DataFrame has only one row or column, you will need to give a specific column or row to get a Series object.
Leave a comment