简体   繁体   中英

Python - Using row Series from iterrows()

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...

If you are working on dataframe, try this:

df[“Name”].value_counts()

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

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM