Home > Software engineering >  Select row in dataframe with column more than 1 values
Select row in dataframe with column more than 1 values

Time:12-01

For example I have this dataframe :

A            Variant&Price                Qty
AAC           7:124|25: 443                1
AAD             35:|35:                    1
AAS           32:98|3:40                   1
AAG             2: |25:                    1
AAC          25:443|26:344                 1

And I want to select row which has type 7 and below, so then my dataframe will look like this

A            Variant&Price                 Qty
AAC           7:124|25: 443                1
AAS            32:9|3:40                   1
AAG             2: |25:                    1

: is separator between type and price, 12:250 means type 12 with price 250. | is separator of item. I dont know how to deal with column with more than one value. I am trying to change its type to string but then how do I select 7 and below ? also the data that w are interested is only one digit before : and one digit after |.

CodePudding user response:

str method is very useful in your case: like that we can split the type/price column into 4 parts. Then we take the minimum elements from first and third parts (the types) and if it is lower than 7, we take it in our final result.

from pandas import DataFrame

df = DataFrame([['AAC', '7:124|25:443', 1],
                ['AAD', '35:|35:', 1],
                ['AAS', '32:98|3:40', 1],
                ['AAG', '2: |25: ', 1],
                ['AAC', '25:443|26:344', 1]],
               columns=['A', 'Variant&Price', 'Qty'])

split_df = df['Variant&Price'].str.split(':|\|', expand=True)
print(df[split_df.iloc[:, [0,2]].astype(int).min(axis=1) <= 7])
  • Related