목록Note (472)
Note
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 create a 2D array containing random floats between 5 and 10? # Input arr = np.arange(9).reshape(3,3) # 1: rand_arr = np.random.randint(low=5, high=10, size=(5,3)) + np.random.random((5,3)) # print(rand_arr) # 2: rand_arr = np.random.uniform(5,10, size=(5,3)) print(rand_arr) # output [[ 8.50061025 9.10531502 6.85867783] [ 9.76262069 9.87717411 7.13466701] [ 7.48966403 8.33409158 6.16808631..
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 reverse the columns of a 2D array? # Input arr = np.arange(9).reshape(3,3) # Solution arr[:, ::-1] # output array([[2, 1, 0], [5, 4, 3], [8, 7, 6]])