Pandas tricks for Data Scientists

Pandas make life easier for any data scientist

sampath kumar gajawada
Nov 2 · 4 min read

Data Science is manipulating data, looking for patterns, and coming up with solutions to drive revenue, lower expenses, and thereby increase overall business profitability ― Ken Poirot

image by Stan W.

import pandas as pddf = pd.read_csv("weather.csv")
df
weather data

1. Reverse the order

df.loc[::-1]
reverse in rows

2. Filter Data by multiple categories

df[df.event.isin([‘Rain’,’Sunny’])]
Filtered data

3. Split column value to multiple columns

create a data frame with the city and its attributes

df1 = pd.DataFrame({‘city’:[‘new york’,’mumbai’,’paris’] , ‘temp_windspeed’: [[21,4],[34,3],[26,5]]})
weather data
df2 = df1.temp_windspeed.apply(pd.Series)
df2.rename(columns= {0:'temperature',1:'windspeed'})
After splitting

4. Combine two data frames

Create two data frames

df1 = pd.DataFrame({
“city”: [“new york”,”florida”,”mumbai”],
“temperature”: [22,37,35]
})
df2 = pd.DataFrame({
“city”: [“chicago”,”new york”,”florida”],
“temperature”: [35,28,25]
})

Concat the created data frames

pd.concat([df1, df2],ignore_index=True)
After combining

5. Pivot

df.pivot(index=’city’,columns=’day’,values=”temperature”)
Pivot data

6. Reshape

header = pd.MultiIndex.from_product([[‘2018’,’2019'],[‘Physics’,’Chemistry’,’Maths’]])
data=([[31,45,65,43,32,65],[76,56,78,65,78,65],[44,56,73,76,87,56]])
df = pd.DataFrame(data,
index=[‘John’,’Gil’,’Gina’],
columns=header)
Students data
df.stack()
After reshape

References

Hope you enjoyed it !!! Stay tuned !!! I will try to collect/create more tricks as possible and come with another article having amazing tricks !!!!. Please do comment on any queries or suggestions !!!!!

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

sampath kumar gajawada

Written by

Machine learning Enthusiast | Analyst | Programmer | All I write my own

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade