Pandas notes
Some notes/code snippet related to pandas operations.
Apply a function with multiple arguments to pandas DataFrame columns - using DataFrame.apply()
DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)import pandas as pd
def date_to(row, col_name):
''' Function to convert an ISO date format to standard date format'''
row[col_name] = pd.to_datetime(row[col_name]).strftime('%Y-%m-%d %H:%M%:%S.%f')
#row[col_name] = pd.to_datetime(row[col_name]).strftime('%Y-%m-%d %H:%M%:%S.%f')[:-3]
return row[col_name]
sample_dates = {
"Date": [
"2021-04-12T07:21:00.345344",
"2015-10-19T07:18:00.234234",
"2018-11-19T07:15:00.423423",
],
"AnotherDate": [
"2018-12-22T07:21:00.963344",
"2019-08-19T07:18:00.887234",
"2020-03-20T07:15:00.765423",
]
}
df = pd.DataFrame(s2)
# df['Date'] = df.apply(date_to, axis=1) :apply function without paramter.
df["Date"] = df.apply(date_to, args=("Date",), axis=1) # apply function with paramter.
df["AnontherDate"] = df.apply(date_to, args=("AnontherDate",), axis=1)Output:
Last updated