根据熊猫中一列中的逻辑测试从DataFrame中选择行

时间:2018-06-24 18:36:02

标签: python pandas dataframe boolean

考虑此数据框

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']})

我想对export_services上的布尔值进行过滤(丢弃空列表),并仅输出列的子集

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

我将如何处理?

谢谢:)

2 个答案:

答案 0 :(得分:2)

将列转换为布尔值,返回False以获得空值,因此可以使用loc进行过滤:

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

答案 1 :(得分:2)

通过使用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