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
130 views
in Technique[技术] by (71.8m points)

python - Accessing total_seconds() in pandas data column

I want to create a new column in a pandas data frame that is the elapsed time from the start of the data frame. I am importing a log file into a data frame which has datatime info, but accessing the total_seconds() function in s_df['delta_t'] is not working. It works if I access the individual elements of the column (s_df['delta_t'].iloc[8].total_seconds()), but I want to create a new column with total_seconds() and my attempts are failing.

s_df['t'] = s_df.index  # s_df['t] is a column of datetime
s_df['delta_t'] = ( s_df['t'] - s_df['t'].iloc[0]) # time since start of data frame
s_df['elapsed_seconds'] = # want column s_df['delta_t'].total_seconds()
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

use .dt accessor:

s_df['elapsed_seconds'] = s_df['delta_t'].dt.total_seconds()

Example:

In [82]:
df = pd.DataFrame({'date': pd.date_range(dt.datetime(2010,1,1), dt.datetime(2010,2,1))})
df['delta'] = df['date'] - df['date'].iloc[0]
df

Out[82]:
         date   delta
0  2010-01-01  0 days
1  2010-01-02  1 days
2  2010-01-03  2 days
3  2010-01-04  3 days
4  2010-01-05  4 days
5  2010-01-06  5 days
6  2010-01-07  6 days
7  2010-01-08  7 days
8  2010-01-09  8 days
9  2010-01-10  9 days
10 2010-01-11 10 days
11 2010-01-12 11 days
12 2010-01-13 12 days
13 2010-01-14 13 days
14 2010-01-15 14 days
15 2010-01-16 15 days
16 2010-01-17 16 days
17 2010-01-18 17 days
18 2010-01-19 18 days
19 2010-01-20 19 days
20 2010-01-21 20 days
21 2010-01-22 21 days
22 2010-01-23 22 days
23 2010-01-24 23 days
24 2010-01-25 24 days
25 2010-01-26 25 days
26 2010-01-27 26 days
27 2010-01-28 27 days
28 2010-01-29 28 days
29 2010-01-30 29 days
30 2010-01-31 30 days
31 2010-02-01 31 days

In [83]:
df['total_seconds'] = df['delta'].dt.total_seconds()
df

Out[83]:
         date   delta  total_seconds
0  2010-01-01  0 days              0
1  2010-01-02  1 days          86400
2  2010-01-03  2 days         172800
3  2010-01-04  3 days         259200
4  2010-01-05  4 days         345600
5  2010-01-06  5 days         432000
6  2010-01-07  6 days         518400
7  2010-01-08  7 days         604800
8  2010-01-09  8 days         691200
9  2010-01-10  9 days         777600
10 2010-01-11 10 days         864000
11 2010-01-12 11 days         950400
12 2010-01-13 12 days        1036800
13 2010-01-14 13 days        1123200
14 2010-01-15 14 days        1209600
15 2010-01-16 15 days        1296000
16 2010-01-17 16 days        1382400
17 2010-01-18 17 days        1468800
18 2010-01-19 18 days        1555200
19 2010-01-20 19 days        1641600
20 2010-01-21 20 days        1728000
21 2010-01-22 21 days        1814400
22 2010-01-23 22 days        1900800
23 2010-01-24 23 days        1987200
24 2010-01-25 24 days        2073600
25 2010-01-26 25 days        2160000
26 2010-01-27 26 days        2246400
27 2010-01-28 27 days        2332800
28 2010-01-29 28 days        2419200
29 2010-01-30 29 days        2505600
30 2010-01-31 30 days        2592000
31 2010-02-01 31 days        2678400

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

...