2

Consider this dataframe

my_input_df = pd.DataFrame({
'export_services': [[1],[],[2,4,5],[4,6]], 
'import_services': [[],[4,5,6,7],[],[]], 
'seaport':['china','mexico','africa','europe'], 
'price_of_fish':['100','150','200','250'],
'price_of_ham':['10','10','20','20']})

And I want to do a filter on the export_services that is boolean (discards empty lists) and output only a subset of columns

my_output_df = pd.DataFrame({
'export_services': [[1],[2,4,5],[4,6]], 
'seaport':['china','africa','europe'], 
'price_of_fish':['100','200','250']})

How would I go about this?

Thanks :)

2 Answers 2

2

Convert column to boolean, what return Falses for empty values, so is possible use loc for filtering:

df = my_input_df.loc[my_input_df['export_services'].astype(bool), 
                     ['export_services','seaport','price_of_fish']]
print (df)
  export_services seaport price_of_fish
0             [1]   china           100
2       [2, 4, 5]  africa           200
3          [4, 6]  europe           250
Sign up to request clarification or add additional context in comments.

Comments

2

By using str.len

my_input_df.loc[my_input_df.export_services.str.len()>0,].drop(['import_services','price_of_ham'],1)
Out[220]: 
  export_services price_of_fish seaport
0             [1]           100   china
2       [2, 4, 5]           200  africa
3          [4, 6]           250  europe

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.