Prerequisistes: Python Pandas
Pandas is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.
In this article, we are trying to filter the data of an excel sheet and save the filtered data as a new Excel file.
Note: You can click on this filename to download this sheet datasets.xlsx
Excel Sheet used:
In this excel sheet we are having three categories in Species column-
Now our aim is to filter these data by species category and to save this filtered data in different sheets with filename =species.subcategory name i.e. after the execution of the code we will going to get three files of following names-
Below is the implementation.
- First, we have imported the Pandas library.
- Then we have loaded the data.xlsx excel file in the data object.
- To fetch the unique values from that species column we have used
unique()function. To check the unique values in the Species column we have called the unique() in speciesdata object.
- Then we will going to iterate the speciesdata object as we will going to store the Species column unique values(i.e. Setosa, Versicolor, Virginica) one by one.
- In object “a” we are filtering out the data that matches the
Species.speciesdatai.e. in each iteration object a will going to store three different types of data i.e. data of Setosa type then data of Versicolor type and at last the data of Virginica type.
- Now to save the filtered data one by one in excel file we have used to_excel function, where, the file will going to be saved by the speciesdata name.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.