0

I'm trying to drop some rows from my dataframe according to a specific element if the elements length is different than 7 then this element should be discarded.

for i in range(len(ups_df['Purchase_Order'])):
        if( len(ups_df['Purchase_Order'][i]) != 7):
            del ups_df['Purchase_Order'][i]

print(len(ups_df['Purchase_Order']))

The output I get is key error 4

  • Wellcome to StackOverflow, could you show an example of your dataframe and your expected output? – ansev Nov 28 '19 at 14:04
  • Thank you very much :) I cannot show here my dataframe because it has data from my work. What I'm trying to do is to drop or delete the rows that contain the elements that have length above 7. For example if there are two rows with elements working in the first and work in the second, I want the second to be discarded – Alexandros Giannakakis Nov 28 '19 at 14:14
  • Use `ups_df[ups_df['Purchase_order'].astype(str).str.len() == 7)]` – jezrael Nov 28 '19 at 14:16

1 Answers1

0

I would solve this issue using lambda to filter the specific row in the dataframe with the condition, to keep only rows that have a length of 7. You can obviously change or adapt this to fit your needs:

filtered_df = ups_df[ups_df['Purchase_order'].apply(lambda x: len(str(x)) == 7)]

This is an example:

data = {'A':[1,2,3,4,5,6],'Purchase_order':['aaaa111','bbbb222','cc34','f41','dddd444','ce30431404']}
ups_df = pd.DataFrame(data)
filtered_df = ups_df[ups_df['Purchase_order'].apply(lambda x: len(str(x)) == 7)]

Original dataframe:

   A Purchase_order
0  1        aaaa111
1  2        bbbb222
2  3           cc34
3  4            f41
4  5        dddd444
5  6     ce30431404

After filtering (dropping the rows that have a length different than 7):

   A Purchase_order
0  1        aaaa111
1  2        bbbb222
4  5        dddd444
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53