简体   繁体   English

Python - 使用 iterrows() 中的行系列

[英]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.我正在尝试将 iterrows() 用于 DataFrame...该列可能有一个值,例如 Fred、William、John,我想计算列出了多少个名字。 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:但是,当我尝试实际使用 len() 的值时,它会给我错误...例如:

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..那会给我一个错误.. 'float' object 没有属性 'split' 之类的..

And when I do:当我这样做时:

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

It will give the error: 'str' object has no attribute 'str'它会给出错误: '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:如果您正在处理 dataframe,请尝试以下操作:

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参考 - pandas 根据其他列的值创建新列/按行应用多列的 function

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.您可以改用count

search_df['Name'].apply(lambda x: str(x).count(',')+1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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