'if satatement in pandas dataframe

I have dataframe that by using a groupby function I divide them.

import pandas as pd
df = pd.read_excel("/content/project.xlsx")
common = df.groupby("col_3")
split = dict(iter(common))
list1 = []
for i, g in df.groupby('col_3'):
  list1.append(i)

Now I want with using for loop apply some changes on the dataframes that I extract like b:

for m in list1:
  df3= split[m]
  if df3.loc[df3.col_8== 1]:
    df1 = df3.drop(["col_11"], axis=1)
  else:
    df2= df3[["col_8","col_9","col_10","col_11"]]

I got this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Why is that?

This is my dataframe:

enter image description here



Solution 1:[1]

df1 = df3[df3['col_8'] == 1]
df1.drop(['col_11'], inplace=True, axis=1)

df2 = df3[df3['col_8'] != 1]
df2 = df2[['col_8', 'col_9', 'col_10', 'col_11']].copy()

No for loop needed just filter. First off, .loc[this needs to be an integer val for index]. You are also comparing a single value to an entire column. When you say something like df3.col8 you are referring to an entire column so you couldn't compare it to a single int such as 1.

Ouputs:

  col2  col_3   col_4  col_8  col_9    col_10
0    a    123   89562      1     85      jack
3    v    252    5459      1      8      rose
5    z    698  988592      1   5787  jennifer
6    b    654   56562      1   5665    androw

   col_8  col_9  col_10 col_11
1      2     22    bill      t
2      3     22  amanda      f
4      2   5652   marry      f
7      2    564   james      t
8      3    654    sara      f

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1