pandas - Select colums in a csv file based on values in python -


i have list of item numbers follows.

item_numbers = [1,2,5] 

i have csv file contains ingredients of item_numbers.

,sugar, protein, salt, oil 0, 0.2, 0.3, 0,   0 1, 0,    0,  0.2, 0.8 2, 0.4,  0,  0,   0 

now, want ingredients items in list value greater 0 (if value == 0, don't need ingredient)

e.g., item 1 in 'item_numbers' list -> [{'salt': 0.2}, {'oil': 0.8}] 

my current code follows.

df = pd.read_csv(ingredients, sep = ',') df = df.iloc[:,1:] df = df.loc[item_numbers].dropna(how='all').gt(0).apply(lambda x: x.index[x].tolist(), 1) ingredients = df.values.tolist() print(ingredients) 

please me.

you can use:

df = df.loc[item_numbers].dropna(how='all').apply(lambda x: x[x > 0].to_dict(), 1) ingredients = df.values.tolist() print(ingredients)  [{'oil': 0.80000000000000004, 'salt': 0.20000000000000001}, {'sugar': 0.40000000000000002}] 

for remove float precision numbers possible use:

  1. convert values str:
df = df.loc[item_numbers].dropna(how='all').apply(lambda x:x[x > 0].astype(str).to_dict(), 1)  ingredients = df.values.tolist() print(ingredients) [{'oil': '0.8', 'salt': '0.2'}, {'sugar': '0.4'}] 
  1. multiple 10, 100 , divide back:
df = df.loc[item_numbers].dropna(how='all').mul(10).apply(lambda x: x[x > 0].to_dict(), 1)  ingredients = df.values.tolist() print(ingredients) [{'oil': 8.0, 'salt': 2.0}, {'sugar': 4.0}] 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -