Reputation: 708
This the dataframe code:
import pandas as pd
storage = pd.DataFrame({'red_wire':[2,2,8,1,5],'white_wire':[5,5,3,5,2],'black_wire':[0,10,4,2,1]})
And the dataframe structure with data:
red_wire white_wire black_wire
0 2 5 0
1 2 5 10
2 8 3 4
3 1 5 2
4 5 2 1
Consider we have two dictionaries. They have tags outside with the color of the wires that must be inside the box:
box1 = {'red_wire':2,'white_wire':5,'black_wire':10}
box2 = {'red_wire':2,'white_wire':5,'black_wire':0}
I made this code to print the boxes contents, my intention is to control and print the wire codes, some boxes may have distinct color tags. That's why I came out with this code:
for key,value in box1.items(): #getting box tags and quantities
col_name = [column for column in storage.columns if key == column] #columns in storage df
for x,i in zip(storage.columns,storage.index): #accesing data in dataframe
if x in col_name and value:
print(x,col_name,value)
The output is :
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
black_wire ['black_wire'] 10
calling the box2:
for key,value in box2.items():
col_name = [column for column in storage.columns if key == column]#columns in dict
for x,i in zip(storage.columns,storage.index):#accesing data in dataframe
if x in col_name and value:
print(x,col_name,value)
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
In box2 I was expecting to get black_wire ['black_wire'] 0, but it assumes it is a value to skip. I want to get the prints of the 0 articles in the dictionary evaluation and only skip values if the tag is not present on the box.
Some users provided me good solutions to get the 0 with the 'is not None' after the conditional sentence:
if x in col_name and value is not None:
It prints:
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
black_wire ['black_wire'] 0
But what if I ask specifically by 0:
for key,value in box2.items():
col_name = [column for column in storage.columns if key == column]#columns in dict
for x,i in zip(storage.columns,storage.index):#accesing data in dataframe
if x in col_name and value == storage[x][i]:
print(x,col_name,value)
It prints:
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
"is not None", doesn't make any difference
#is not None 1st
if x in col_name and value is not None and value == storage[x][i]:
print(x,col_name,value)
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
#is not None after
if x in col_name and value == storage[x][i] and value is not None:
print(x,col_name,value)
red_wire ['red_wire'] 2
white_wire ['white_wire'] 5
Upvotes: 2
Views: 130
Reputation: 4487
You have 2 issues in your source code.
In your first version, you used value
in your conditional, and like said in comments, bool(0) == false
, so you add to specify is not None
to manage the fact there is a 0 value, and there is NO value at all.
In your final version, you are iterating on columns, using the Panda indexing system, called i
in your loop:
for x,i in zip(storage.columns,storage.index):#accesing data in dataframe
But you use this index to seek the value in corresponding Dataframe:
value == storage[x][i]
You are lucky it works for red_wire
, and white_wire
, and even black_wire
for the box1
.
Maybe you want to ensure the value belongs to the corresponding Dataframe part; in which case you can change your source code this way:
value in storage[x].values
This way you don't need at all to check if value if not None; and you don't need either to iterate in index
; your source code could be simplified to:
for x in storage.columns:
if x in col_name and value in storage[x].values:
Let me know if it reaches you needs.
Upvotes: 2