Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

Create new column datetime with add minutes (Pandas, Python)

I newest in Pandas, Please help with the next issue:

  1. I get the datatable from MS SQL DataBase, like:
(datetime.datetime(2020, 12, 1, 0, 0), 'ECS446_FSL_969_D01_F41', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60', '60'), **...**)
  1. Input Data in Pandas DataFrame and did next step:

Columns '0'...'59' is a minutes in hour('DataTime') with values of 'TagName'. Did next transformations:

df1 = pd.DataFrame(result2, columns=['DataTime', 'TagName', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59'])
df1 = df1.pivot(index='TagName', columns= 'DataTime', values=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11', '12', '13', '14', '15', '16', '17', '18', '19','20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49','50', '51', '52', '53', '54', '55', '56', '57', '58', '59'])
df1 = df1.T
df1 = df1.sort_values('DataTime')

And have to result:

picture1

  1. The problem is to add minutes to the DateTime column, I want to get the following result:

picture2

question from:https://stackoverflow.com/questions/65896616/create-new-column-datetime-with-add-minutes-pandas-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use:

#sample data
a = [pd.to_datetime('2020-12-01'), 'code1'] + [60] * 60
b =  [pd.to_datetime('2020-12-01 10:00:00'), 'code2'] + [5] * 60
result2  = [a, b]

cols = ['DataTime', 'TagName', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]

df = pd.DataFrame(result2, columns=cols)

First unpivot by DataFrame.melt, add minutes to datetimes by to_timedelta and last pivoting:

df1 = df.melt(['DataTime', 'TagName'], var_name='minutes', value_name='data')

df1['DataTime'] += pd.to_timedelta(df1['minutes'], unit='Min')
print (df1)

df2 = df1.pivot('DataTime','TagName','data')
print (df2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...