Table of Contents
Pandas
Pandas Conditonal Column
A conditional column is a column in a dataframe that is generated based on other column/s. There are multiple way of doing this in Python but one of the quickest ways is to use the np.where()
function. This function is basically the equivalent of an if statement in Excel.
np.where(condition,True action, False action)
|
|
If we print the first 5 rows of our data frame we get :
col1 odd/even
0 87 odd
1 85 odd
2 22 even
3 80 even
4 0 even
Applying functions to Pandas columns
We want to manipulate all the values in one column with a function with created. It’s actually quite simple as Pandas series have an .apply
method. An example below
|
|
If we print the first 5 rows of our data frame we get :
|
|
Converting strings to date or datetime
It would be nice if the whole word worked with ISO dates. Sadly that is not the case, if you’ve read in a dataframe and want to convert a column to date time. For example, you’ve read in the data and the dataframe looks like this:
|
|
We can simply convert this to date using pandas.to_datetime
function. You simply pass the series and format you need to convert
|
|
To determine the format, you can use this table
Code | Meaning | Code | Meaning |
%a | Abbreviated weekday | %A | Full weekday |
%b | Abbreviated month | %B | Full month |
%c | Locale-specific date and time | %d | Decimal date |
%H | Decimal hours (24 hour) | %I | Decimal hours (12 hour) |
%j | Decimal day of the year | %m | Decimal month |
%M | Decimal minute | %p | Locale-specific AM/PM |
%S | Decimal second | %U | Decimal week of the year (starting on Sunday) |
%w | Decimal Weekday (0=Sunday) | %W | Decimal week of the year (starting on Monday) |
%x | Locale-specific Date | %X | Locale-specific Time |
%y | 2-digit year | %Y | 4-digit year |
%z | Offset from GMT | %Z | Time zone (character) |