Pandas Dataframe, Skip Index If Condition Is Meet
I want to skip the index of a data frame output only if specific conditions are met. A it's a special id for seller. B it's the selling price. Type is selling type. import pandas
Solution 1:
Use:
import pandas
Data= []
Data+=[{"A": "X1FFFFF", "Special":{"type": "USD"},"B": "0.11",}]
Data+=[{"A": "X2FFFFF", "Special":{"type": "EUR"},"B": "0.122",}]
Data+=[{"A": "X3FFFFF", "Special":{"type": "EUR"},"B": "0.1444",}]
#all data in Special are strings, simulate by astype(str)
Data = pandas.DataFrame(Data).astype(str)
Data = Data.astype({"B": float})
Data = Data.astype({"A": str})
import ast
#convert to dicts if necessary
Data["Special"] = Data["Special"].apply(ast.literal_eval)
#filtering `type` for `EUR`
euro_rows = Data.loc[Data["Special"].str.get('type') == 'EUR']
#get minimal value and index (A) by minimal
lowest_price = euro_rows.set_index('A')['B']
seller = lowest_price.idxmin()
price = lowest_price.min()
print(seller)
X2FFFFF
print(price)
0.122
Solution 2:
I think you can do something like this:
# gets only the Euro type
euro_rows = Data.loc[Data["Special"] == {'type': 'EUR'}]
# gets the row of the lowest price
lowest_price = euro_rows.loc[euro_rows['B'] == euro_rows['B'].min()]
# gets seller and price
seller, price = lowest_price['A'].item(), lowest_price['B'].item()
print(seller) #prints: IDDDDDDDDDDDprint(price) #prints: 0.1
Post a Comment for "Pandas Dataframe, Skip Index If Condition Is Meet"