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

python - pandas series find index of a certain type

one of the columns of my df contains mostly datetime.time types

all_cities['Result'].head()
0    02:19:53
1    02:20:10
2    02:20:52
3    02:37:19
4    02:38:05

Name: Result, dtype: object

but, all_cities['Result'][0]

datetime.time(2, 19, 53)

how can i find the index of cell of different types(not datetime.time)?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Say you have

df = pd.DataFrame({'ex': [1, datetime.time(2,19,53), "string", [1,2,3]]})

    ex
0   1
1   02:19:53
2   string
3   [1, 2, 3]

You can do

df[df.ex.transform(type) == datetime.time]

To get

ex
1   02:19:53

Details:

transform(type) yields the type of your entries

0              <class 'int'>
1    <class 'datetime.time'>
2              <class 'str'>
3             <class 'list'>

Then

 df.ex.transform(type) == datetime.time

0    False
1     True
2    False
3    False

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

...