Useful Pandas code snippets [Python]

python pandas logo
Spread the word!

Code Snippet #1

Scenario: Data preparation. The initial dataset has string values (also Timestamp). Some of them are written in form of “[number] [unit]” and need to be transformed into a number. The Timestamp will be considered as index. All non numeric values must be converted into NaN.

Using such settings and functions, let’s see how a sample dataframe is manipulated.

Initial DataFrame:

pandas dataframe 1

After splitting values from units:

pandas dataframe 2

Changing column names:

panda dataframe 3

Code Snippet #2

Plot all dataframe columns values against their timestamp

Output example

plot pandas dataframe

Code Snippet #3

Reorganize your dataframe with fixed time steps (e.g. one data row per day) and applying (different) functions to each column within such time step (e.g. sum(), mean(), max(), min() of the values falling into a single time step). Columns for which no function is explicitly set will be ignored and not included in the result.

Output:

[… to be continued …]

Be the first to comment

Leave a Reply

Your email address will not be published.


*