0

I have a pandas data frame as below. This data for one month period. I need to select devices who start from ABC. I need to get my data frame too. ( as my expected output). How do I do it?

Time Stamp              Id  Latitude    Longitude  Device
01/10/2016 15:22:51:700 1      23        50        ABC (aaa)
01/10/2016 16:28:08:026 1      23        50        ABC (aaa)
01/10/2016 16:28:09:026 1      12        45        ABC (bae)
02/10/2016 19:00:08:026 2      23        50        TTT (ff)
02/10/2016 20:28:08:026 1      23        50        GGG (lll)
03/10/2016 19:00:08:000 2      23        50        ABC (zzz)
03/10/2016 01:02:33:123 2      23        50        NNN (gg)
03/10/2016 06:15:08:500 1      23        50        KKK (yyy)
03/10/2016 10:01:07:022 3      28        88        ABC (bae)
......
......
31/10/2016 13:09:17:044 1      33        80        ABC (bae)

My expected output is:

 Time Stamp             Id  Latitude    Longitude  Device
01/10/2016 15:22:51:700 1      23        50        ABC (aaa)
01/10/2016 16:28:08:026 1      23        50        ABC (aaa)
01/10/2016 16:28:09:026 1      12        45        ABC (bae)
03/10/2016 19:00:08:000 2      23        50        ABC (zzz)
03/10/2016 10:01:07:022 3      28        88        ABC (bae)
......
......
31/10/2016 13:09:17:044 1      33        80        ABC (bae)
User1
  • 107
  • 4
  • 15
  • The other answers related to this question are not what I want. Those answers weren't help for me. – User1 Jan 18 '18 at 19:06

2 Answers2

2

Try this: df[df.Device.str.startswith('ABC')]

With pandas, you can use series.str to utilize several useful functions for string manipulation.

Series.str.startswith, the one I showed above, is to select (return bool values) indices that starts with a specific pattern.

BTW, the answer using contains with ^ is basically the same thing but, instead of using a function provided by pandas, it uses regex to define the pattern on what should be the first character of your input string.

Zhiya
  • 610
  • 2
  • 7
  • 22
1

Assuming your dataframe is called "df", how about using the regexp matching facilities in pandas.Series.str:

df[df['Device'].str.contains('^ABC.*')]
rwp
  • 1,786
  • 2
  • 17
  • 28
  • Hi, when I do the selection for devices '^Apple iPhone 6 (A1586)', as a result I got empty data frame. I did exact your code. Why I'm getting empty df? ( I need exactly this device only) – User1 Jan 19 '18 at 14:45