Ison
Ison

Reputation: 403

Troubles with conditional looping

I have an initial dataframe:

 A | B | C | D        |  E
one| AA| 4 | 24.07.19 |25.07.19
two| AA| 1 | 24.07.19 |25.07.19
one| AB| 3 | 24.07.19 |25.07.19
two| AC| 1 | 24.07.19 |25.07.19
two| AD| 1 | 24.07.19 |25.07.19
one| AD| 2 | 24.07.19 |25.07.19

Items in B column can have only two parameters from A column (one, two). I'm trying to create plots for each item in B column.
The idea is, that IF item has both two parameters, do one kind of plot if no => another.

But I stuck on the problem that whenever I try to make a condition I receive or The Truth value is ambiguous error or only the else-part works even if the item has two parameters.

How can it sound in pseudocode:

IF item has 'one' AND 'two' in A column:
do smth
ELSE (IF item has 'one' OR 'two' in A column):
do another

My code is:

items = df['A'].unique()

for item in items:
    aa= df[df['B']==item][df['A'].isin(['one','two'])].reset_index(drop=True)
    aa = aa[aa['C']<500].reset_index(drop=True)
    if (aa['A'].values[0]=='one') & (aa['A'].values[0]=='two'):
       print('yes')
    else:
       print('no')

Thanks in advance for the help

Upvotes: 0

Views: 50

Answers (2)

zipa
zipa

Reputation: 27869

I believe this will get you out of the error:

aa = df[(df['B']==item)&(df['A'].isin(['one','two']))].reset_index(drop=True)

But you still have:

if (aa['A'].values[0]=='one') & (aa['A'].values[0]=='two'):
    ...

Which is never True as you are testing same thing to be both one and two.

That being said you might want to go with or there:

if (aa['A'].values[0]=='one') | (aa['A'].values[0]=='two'):
    ...

EDIT

I'm guessing this is what you are looking for:

for item in items:
    aa= df[(df['B']==item)&(df['A'].isin(['one','two']))].reset_index(drop=True)
    aa = aa[aa['C']<500].reset_index(drop=True)
    if ('one' in aa['A'].values) & ('two' in aa['A'].values):
       print('yes')
    else:
       print('no')

#yes
#no
#no
#yes

Upvotes: 1

Aryerez
Aryerez

Reputation: 3495

If you want to know how many values of column "A" exist for each value in column "B", you can do:

counts = df[['A', 'B']].drop_duplicates().groupby('B').count().reset_index().rename({'A': 'A_counts'}, axis=1)

Then you can merge counts into df with:

df = df.merge(counts, how='left')

And in df you will have a column A_counts which states for each value in column 'B' how many items of column 'A' it has, so you can do conditions on that new column.

Upvotes: 0

Related Questions