Python Forum
How to Filter a Dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Filter a Dataframe
#5
I doubt S_ID column had whitespaces,

import pandas as pd
df=pd.read_excel(open('b111.xlsx', 'rb'),sheet_name='Sheet1') 
print(df)

#adding len coluumn
df['id_char_length']=df['id'].astype(str).map(len)
print(df)
#remove column
df=df.drop(['id_char_length'],axis=1)

#fiter without trim spaces
df = df[df['id'] != '[]']
print(df)

#filter with trim spaces
df = df[df['id'].str.strip() != '[]']
print(df)
Output:
C:\Users\sandeep\Downloads>py b11 active createdate createtime username id 0 True 2020-11-03 12:04:29 M001AE8 [] 1 True 2020-11-03 12:04:29 M001AE8 [] 2 True 2020-11-03 12:02:35 M001AF0 [] 3 True 2019-06-11 01:32:12 M017A70 [] 4 True 2020-10-22 09:09:06 S345678 ['S345678'] 5 True 2020-10-22 09:09:06 S567456 ['S567456'] 6 True 2020-03-16 07:41:05 S789077 ['S789077'] 7 True 2020-03-16 07:41:05 S123456 ['S123456'] 8 True 2014-07-05 06:38:59 SvcPcc [] 9 True 2014-07-05 06:38:59 SvcPcc [] 10 True 2020-07-27 12:15:24 SvcPcc [] active createdate createtime username id id_char_length 0 True 2020-11-03 12:04:29 M001AE8 [] 10 <<<< 1 True 2020-11-03 12:04:29 M001AE8 [] 2 2 True 2020-11-03 12:02:35 M001AF0 [] 2 3 True 2019-06-11 01:32:12 M017A70 [] 2 4 True 2020-10-22 09:09:06 S345678 ['S345678'] 11 5 True 2020-10-22 09:09:06 S567456 ['S567456'] 11 6 True 2020-03-16 07:41:05 S789077 ['S789077'] 11 7 True 2020-03-16 07:41:05 S123456 ['S123456'] 11 8 True 2014-07-05 06:38:59 SvcPcc [] 21 <<<< 9 True 2014-07-05 06:38:59 SvcPcc [] 2 10 True 2020-07-27 12:15:24 SvcPcc [] 2 active createdate createtime username id 0 True 2020-11-03 12:04:29 M001AE8 [] 4 True 2020-10-22 09:09:06 S345678 ['S345678'] 5 True 2020-10-22 09:09:06 S567456 ['S567456'] 6 True 2020-03-16 07:41:05 S789077 ['S789077'] 7 True 2020-03-16 07:41:05 S123456 ['S123456'] 8 True 2014-07-05 06:38:59 SvcPcc [] active createdate createtime username id 4 True 2020-10-22 09:09:06 S345678 ['S345678'] 5 True 2020-10-22 09:09:06 S567456 ['S567456'] 6 True 2020-03-16 07:41:05 S789077 ['S789077'] 7 True 2020-03-16 07:41:05 S123456 ['S123456']
Best Regards,
Sandeep.

GANGA SANDEEP KUMAR
Reply


Messages In This Thread
How to Filter a Dataframe - by mfkzolo - Nov-13-2020, 12:59 PM
RE: How to Filter a Dataframe - by aajkaal - Nov-14-2020, 12:37 AM
RE: How to Filter a Dataframe - by mfkzolo - Nov-16-2020, 05:14 AM
RE: How to Filter a Dataframe - by mfkzolo - Nov-16-2020, 05:31 AM
RE: How to Filter a Dataframe - by sandeep_ganga - Nov-20-2020, 02:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,295 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  Filter value from DataFrame apply a function and save to xlsx zinho 1 2,043 Dec-22-2019, 03:54 PM
Last Post: zinho

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020