Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Working with Pandas

From the Pandas Documentation

pandas is a Python package providing fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. It aims to be the fundamental high-level building block for doing practical, real-world data analysis in Python. Additionally, it has the broader goal of becoming the most powerful and flexible open source data analysis/manipulation tool available in any language. It is already well on its way toward this goal.

Pandas is an extension of the Numpy data model to structure and organize data into DataFrames. A DataFrame resembles a spreadsheet in looks, but is much more powerful.

Additional Tutorials

Create Pandas DataFrame

Create some data in a python dictionary

Use the dictionary to populate the new pandas data frame. Python and Jupyter play very well with Pandas and when requested to print the DataFrame or Series knows how to print in a visually appealing way. Notice the columns have names taken from the Dictionary keys we used to populate the DataFrame. Each row has an index set to an incremented integer by default.

Pandas DataFrame consisting of Series

Pandas Series

A DataFrame has a concept of “index” to label the rows of the DataFrame and column names to label the Columns. We created column names from the Dictionary keys used to initially create the DataFrame.

Printing the column shows additional informatin including the index and data type.

Extracting a row uses the .loc() method on the DataFrame. .loc() standar for location and searches the index for a match.

Can also extract using index location .iloc() method.

Read in some data

Using the path to a specific file we read the data into a Pandas Data Frame. This is something you will use very often if you have data in ASCII column files. Get to know this method well. The method has keywords to help describe how to read the column data including the delimter, number of header rows, and which column is the time stamp. If it can parse the time stamp it will convert to Pandas native time type.

How many data samples do we have? Can use the Python len() function. Notice it returns the number of rows only.

Similar to Numpy, can use the .shape and .size methods on the DataFrame to return metadata about size of the DataFrame.

Print the first five rows

We can get the names of the columns with .columns method

Pandas has a few methods used to inspect the data in the Dataset or Series and present the typical statistical values. Because Pandas plays so well with Jupyter the output is very easy to visualize.

Calculate the correlation coefficients of the DataSet on each Series

Calculate the covariance coefficients of the DataSet on each Series

What type is the time column data?

Get the pressure Series from the DataFrame and sum it up to one value or calcualte the mean.

Get the pressure Series from the DataFrame and use a special method to summarize the values in the Series using what Pandas things is most helpful.

Extract the RH series from the DataFrame. This is a copy of the Series in the DataFrame so changing the values will not change the values in the DataFrame.

Warning, off on a tangent

Pandas has two ways of extracting a Series from a DataFrame: copy and view. A copy is separate from the DataFrame and changes to the copied Series will not affect the DataFrame. Changes to a view will impact the DataFrame. In pandas, whether you get a view or a copy depends on the structure of the DataFrame and, if you are trying to modify a slice, the nature of the modification. Pandas will often warn when using a view vs. a copy, but that may not alwasy be the case. There is discussion to resovle this issue but the fix is not going to happen soon. The best solution is to use .copy() when you want a copy to be explicit.

Calculate a rolling mean over the Series using 10 points. Notice the first 8 values are NaN. There is a default number of values to use to calculate a value. Else it is set to NaN.

By specifically stating the minimum number of points to use when calculating the mean we force it to not fill in so many NaNs. There is at least one value to use in the rollig window so not NaNs. What happens when you change min_periods to a larger number?

Set a range of values in our extracted data to NaN to represent missing data.

Calculate a rolling max on the entire DataFrame.