2

I'm trying to use iterrows() for a DataFrame... The Column could have a value such as Fred,William,John and I want to count how many names are listed. The following code works great...

for index, row in search_df:
     print(len(row["Name"].split(",")))

However, when I try to actually use the value from len(), it will give me errors... Such as:

for index, row in search_df:
    row["Number of Names"] = len(row["Name"].split(","))

That will give me an error.. 'float' object has no attribute 'split' or something..

And when I do:

    row["Number of Names"] = len(row["Name"].str.split(","))

It will give the error: 'str' object has no attribute 'str'

Looks like a string, but it's a float... Try to treat it as a string, it's already a string... Frustration...

jalue
  • 21
  • 2
  • 2
    Welcome to stack overflow! Please have a look at [How to make good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and [edit] your question to provide a sample input and expected output so that we can better understand your task – G. Anderson Aug 24 '21 at 20:56
  • `row["Number of Names"] = len(row["Name"].split(",")) ………… 'float' object has no attribute 'split'` - this sounds like a misplaced parenthese ;) – TheEagle Aug 24 '21 at 21:03

4 Answers4

0

If you are working on dataframe, try this:

df[“Name”].value_counts()
Matt
  • 166
  • 1
  • 2
  • 18
  • This doesn't appear to answer the question asked. Can you expand a little on how this solves the problem of assigning length values to a column in the given row while using iterrows? – G. Anderson Aug 24 '21 at 21:00
0

Nevermind.. I worked it out...

for index, row in search_df:
    num_language = len(str(row["Language"]).split(","))
    search_df.loc[index, "Number of Names"] = num_language
jalue
  • 21
  • 2
  • 1
    You should use `search_df.apply` for this, not a loop – OneCricketeer Aug 24 '21 at 21:43
  • OneCricketeer: Well, that's the thing though.. There could be 10 rows and under the Language column for each row, you can have a number of names grouped together. How else would I check each row of values and update the Number of Languages column for each specific row? – jalue Aug 24 '21 at 21:52
  • The function is applied column-wise. Refer my answer – OneCricketeer Aug 24 '21 at 21:53
0

Dont use a loop

Refer - pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

def count_names(row):
  return len(row['Name'].split(','))

df["Number of Names"] = df.apply(count_names, axis=1)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Ah, okay.. I see now.. Thanks muchly.. Have to say that looks a whole lot cleaner.. heh – jalue Aug 24 '21 at 22:14
0

Splitting on , and then counting the elements seems inefficient to me. You can use count instead.

search_df['Name'].apply(lambda x: str(x).count(',')+1)
Acccumulation
  • 3,491
  • 1
  • 8
  • 12