Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas slicing with various datatypes

I have a column in a dataframe with two data types, like this:

25                3037205
26    2019-09-04 19:54:57
27    2019-09-09 17:55:45
28    2019-09-16 21:40:36
29                3037206
30    2019-09-06 14:49:41
31    2019-09-11 17:17:11
32                3037207
33    2019-09-11 17:19:04

I'm trying to slice it and build a new data frame like this:

26    3037205    2019-09-04 19:54:57
27    3037205    2019-09-09 17:55:45
28    3037205    2019-09-16 21:40:36
29    3037206    2019-09-06 14:49:41
30    3037206    2019-09-11 17:17:11
31    3037207    2019-09-11 17:19:04

I can't find how to slice between numbers "no datetype".

Some ideas?

Thx!

like image 622
user2509954 Avatar asked Dec 03 '19 16:12

user2509954


1 Answers

Another approach:

s = pd.to_numeric(df['col1'], errors='coerce')
df.assign(val=s.ffill().astype(int)).loc[s.isnull()]

Output:

                   col1      val
26  2019-09-04 19:54:57  3037205
27  2019-09-09 17:55:45  3037205
28  2019-09-16 21:40:36  3037205
30  2019-09-06 14:49:41  3037206
31  2019-09-11 17:17:11  3037206
33  2019-09-11 17:19:04  3037207
like image 177
Quang Hoang Avatar answered Oct 18 '22 00:10

Quang Hoang