목록Pandas (57)
Note
How to get the day of month, week number, day of year and day of week from a series of date strings? # Input ser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-06-06T12:20']) # Solution from dateutil.parser import parse ser_ts = ser.map(lambda x: parse(x)) # day of month print("Date: ", ser_ts.dt.day.tolist()) # week number print("Week number: ", ser_ts.d..
How to convert a series of date-strings to a timeseries? # Input ser = pd.Series(['01 Jan 2010', '02-02-2011', '20120303', '2013/04/04', '2014-05-05', '2015-06-06T12:20']) # 1 from dateutil.parser import parse ser.map(lambda x: parse(x)) # 2 pd.to_datetime(ser) # output 0 2010-01-01 00:00:00 1 2011-02-02 00:00:00 2 2012-03-03 00:00:00 3 2013-04-04 00:00:00 4 2014-05-05 00:00:00 5 2015-06-06 12:2..
How to compute difference of differences between consequtive numbers of a series? # Input ser = pd.Series([1, 3, 6, 10, 15, 21, 27, 35]) # Solution print(ser.diff().tolist()) print(ser.diff().diff().tolist()) # output [nan, 2.0, 3.0, 4.0, 5.0, 6.0, 6.0, 8.0] [nan, nan, 1.0, 1.0, 1.0, 1.0, 0.0, 2.0]
How to calculate the number of characters in each word in a series? # Input ser = pd.Series(['how', 'to', 'kick', 'ass?']) # Solution ser.map(lambda x: len(x)) #output 0 3 1 2 2 4 3 4 dtype: int64
How to convert the first character of each element in a series to uppercase? # Input ser = pd.Series(['how', 'to', 'kick', 'ass?']) # 1 ser.map(lambda x: x.title()) # 2 ser.map(lambda x: x[0].upper() + x[1:]) # 3 pd.Series([i.title() for i in ser]) # output 0 How 1 To 2 Kick 3 Ass? dtype: object