Lecture Note
University
University of California San DiegoCourse
DSC 207R | Python for Data SciencePages
2
Academic year
2023
anon
Views
19
Pandas, Frequent Data Operations Leveraging Pandas for Efficient Data Operations Pandas is definitely a name you've heard of if you use Python to work with data. You may manipulate and analyze data with the help of the strong library known asPandas. It is based on Numpy and made to be effective and simple to use. The most popular Pandas data operations for subsetting, filtering, insertion, deletion, and aggregation will be covered in this article. Due to the fact that theyaccelerate all algorithms that employ them, these effective data operations arecrucial. At the end of this tutorial, you should be able to manually choose data from aDataFrame's rows or columns using Pandas techniques, as well as add or removerows or columns and carry out aggregation operations like groupby in DataFrames. Subsetting Data One of the most frequent actions you'll conduct when working with data is subsetting. Pandas allows you to slice out any column by merely supplying its name.For instance, putting df["sensor"] will slice out the "sensor" column from aDataFrame. By supplying a list of the names of the desired columns, such asdf[["sensor1", "sensor2"]], you can also slice out several columns. Filtering Data A common task is to remove rows based on a criterion. Using Pandas' boolean indexing technique, you can exclude rows that satisfy a particular criterion. Forinstance, you can write df[df["sensor2"] > 0] to get a list of all the rows with sensor2larger than 0. Inserting Data By utilizing the name of the new column you want on the left side and the value on the right side, you can add a new column to a DataFrame. For instance, by squaringthe numbers in the "sensor3" column, you can make a new column called "sensor4".On the right side, you can input data in a variety of forms. The simplest way to learnhow this works is to try adding any column using data from a list or array. With Pandas'.loc or location function, you can pinpoint just the row you wish to include in your new data. You'll see that there are exactly as many values on the rightside of the table as there are columns.
Deleting Data A row in a DataFrame may be deleted using the drop function. To specify one or more rows to drop, use df.index. See how the fifth row is missing and the rightDataFrame on the right is smaller. The del function also allows you to delete acolumn by just using its name. There are occasionally columns that are irrelevant toyour particular analysis. For instance, your ratings database may contain timestampsthat you don't want to take into account when doing your research. Just delete themusing the del function to get rid of them. Aggregating Data The Groupby technique, which provides combined statistics about the DataFrame, is quite helpful. With a student ID, you can conduct groupby in Pandas and retrievethe mean test scores for each subject. Hence, if a student took the same topic morethan a few times, you can group them according to that and take the student'saverage performance. The average grade for that student ID across all subjects willappear here. Conclusion To sum up, Pandas includes a wide variety of effective methods that let you play around with your dataset. We've only covered a small portion of the library here, sowe invite you to look around more to learn about other methods for transforming andanalyzing data. After a while, you'll learn that by combining these straightforwardactions, you may create extensive and intricate analytics pipelines that transformraw data.
Pandas, Frequent Data Operations
Please or to post comments