import pandas as pd
import numpy as np
rows = 4
cols = 4
df = pd.DataFrame(np.zeros((rows, cols)))
I used it when I wanted to split
the element extracted from the numpy array and check the number of decimal places.
Since split
is a method of type str
, convert it to str and then get it.
import numpy as np
narr = np.array([1.3334, 5.3343, 7.3322])
n_to_str = np.array2string(x[0])
print(len(n_to_str.split('.')[-1]))
# 4
When you want to connect to the far right
a = [1,2,3]
b = [1,2,3]
bf = pd.DataFrame()
bf['a'] = a
bf['b'] = b
a b
0 1 1
1 2 2
2 3 3
a = [1,2,3]
b = [1,2,3]
bf = pd.DataFrame(a, columns=['a'])
bf.insert(1, "b", b)
a b
0 1 1
1 2 2
2 3 3
Connected together
a = [1,2,3]
b = [1,2,3]
c = [1,2,3]
cols = {
"a":a,
"b":b,
"c":c
}
da = pd.DataFrame(cols)
#Connected horizontally
print(pd.concat([da,da], axis=1, ignore_index=True))
print()
#Connected vertically
# ignore_If index is not specified as True, it will be 012012.
print(pd.concat([da,da], axis=0, ignore_index=True))
0 1 2 3 4 5
0 1 1 1 1 1 1
1 2 2 2 2 2 2
2 3 3 3 3 3 3
a b c
0 1 1 1
1 2 2 2
2 3 3 3
3 1 1 1
4 2 2 2
5 3 3 3
It is a technique to convert to a format that can be handled by python when calculating date and time data.
Converting to timestamp makes it easier to handle when seconds, minutes, and hours move forward.
An example of trying to calculate without doing anything
#Example) 1:59:50 + 0:00::20 = 2:00:10
#When not converting to timestamp
hour = 1
minute = 59
sec = 50
add_hour = 0
add_minute = 0
add_sec = 20
if sec + add_sec > 60 or minute + add_minute > 60 or hour + add_hour > 24:
#Carrying process
else:
#Processing when it does not move up
Datetime string,Convert to timestamp
from datetime import datetime
s = "2020.07.10 22:59:59"
date = datetime.strptime(s, "%Y.%m.%d %H:%M:%S")
print(date)
# 2020-07-10 22:59:59
timestamp = datetime.timestamp(date)
print(timestamp)
# 1594389599.0
Put the timestamp in a list and then return it to a string
from datetime import datetime
t_list = [1594177973.0,1594177974.0,1594177975.0]
print(type(t_list[0]))
# float
#Convert to datetime type and then to string
datetime.fromtimestamp(t_list[0]).strftime("%H:%M:%S")
# '12:12:53'
You can add and subtract to a datetime.datetime object
using datetime.timedelta
Supports calculations for weeks, days, hours, minutes, seconds
import datetime
#Current time
now = datetime.datetime.now()
print(now)
# 2020-07-11 18:19:47.149523
#Add 10 seconds to the current time
now_add_10sec = now + datetime.timedelta(seconds=10)
print(now_add_10sec)
# 2020-07-11 18:19:57.149523
Use numpy convolve
. It's faster than pandas rollin
, and the code is short and easy to understand.
period = 3 #Average period
data = np.arange(1,10)
print(data)
print(len(data))
# mode = { same, valid, full }You can choose from
# same:The number of elements in the calculation result is the same size(not recommended)
# valid:There are many considerations, but the size is(period-1)Be scraped(Recommendation)
# full:Increased size (deprecated))
average = np.convolve(data, np.ones(period)/period, mode='valid')
print(average)
print(len(average))
print([np.nan]*(period-1) + average.tolist()) #Put Nan in the scraped area
[1 2 3 4 5 6 7 8 9]
9
[2. 3. 4. 5. 6. 7. 8.]
7
[nan, nan, 2.0, 3.0, 3.9999999999999996, 5.0, 6.0, 7.0, 8.0]
Recommended Posts