My Top 10 Most Frequently Used Python Panda Code
There’s a lot of information out there about how to use the python ‘Pandas’ library but most of it is scattered about. This blog organized my Top 10 most frequently used ones. So without further ado, let’s get straight into it!
- Export DF to excel
In various occasions data analysts have to export dataframes to a familiar format so other teammates can see what you’ve been working on. As excel is mostly used, this snippet can easily change your dataframe to an excel format.
2. Export DF to csv
Pretty similar to excel but this downloads the file to a csv format.
3. Export DF to excel but making each file name unique by adding downloaded time to file name
If the name of your downloaded excel file is the same, it will be overwritten by your new downloaded file. The above code saves the date when you downloaded your file on your file name, making the file unique and thus prevent it from being overwritten.
4. Import an excel file as a DF
df = pd.read_excel(r'/Users/user/Downloads/excel1.xlsx')
You have to define where your excel file path is in the [r’/Users/user/Downloads/] part. In this particular case, my excel1 file exists in my Download file so I tracked down the path and coded it in to read the file.
5. Organize DF in descending order of particular column
If you change “ascending=False” to “ascending=True” it will be ascending order.
6. Check rows where a certain word exists
This will show a result of rows where the word ‘winter’ exists within column1.
7. Show unique values of column
If your ‘id’ column has multiple rows of the same value, this code will show only unique values.
8. Drop columns
df.drop(columns=['column1', 'column2], inplace=True)
This code lets you get rid of columns you don’t want. You can get rid of multiple columns in one go.
9. Filter your DF with number condition
df[df.bodylength >= 50]
This will find all rows where the column ‘bodylength’ has a number of over 50.
10. Make a new DF from previous DF with only certain columns
df_new = df[['column1', 'column2', 'column3']]
If your previous DF has maybe 6 columns but you want to make a new DF with only the first 3, you can define your column names and make a new DF with the above code.
These are a few snippets of python code I use frequently on a day to day basis. I hope it helps anyone who is getting started to code in python or anyone who is simply curious about pandas. Cheers :)