Issue
I have a dataframe DF
Name | Code | Color |
---|---|---|
Test | 1 | red |
Test2 | 1 | blue |
Test3 | 2 | red |
Test4 | 3 | blue |
Test5 | 3 | black |
I did the following code :
DFN = DF.loc[DF['Code'].isin(['1'])].reset_index(drop=True)
DFCode = DF.loc[DF['Color'].isin(DFN['Color'].values.tolist())].reset_index(drop=True)
This does return nothing, what am I doing wrong ? The output excepted would be :
Name | Code | Color |
---|---|---|
Test | 1 | red |
Test2 | 1 | blue |
Test3 | 2 | red |
Test4 | 3 | blue |
Note : I don't know the color that I will be looking for and this is why I need to identify the Colors using the Code
Column
Solution
As in the comments already suggested, you need to check if the values in the Code
columns are integers or strings. Probably there is the problem.
If you just look for one value you can just filter with ==
, no need for isin
.
Use pandas.unique
to check for all occuring values in a column, it's just a little more straightforward than values.tolist()
.
DFN = DF[DF['Code']==1] # or DFN = DF[DF['Code']=='1']
DFCode = DF.loc[DF['Color'].isin(DFN['Color'].unique())]
print(DFCode)
Name Code Color
0 Test 1 red
1 Test2 1 blue
2 Test3 2 red
3 Test4 3 blue
Answered By - Rabinzel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.