英文:
I want to spilt a string and then count the value of solumn in dataframe
问题
我有一个名为"control stores"的DataFrame列:
我想使用';'作为分隔符拆分字符串并计算字符串数量。而且,在存在NAN或1个值的情况下,应将计数显示为1。
我尝试了以下代码:
final_input_optimization['Control Stores'].str.split(';')[0].count('Control Stores')
但结果为0,但应该为6。
英文:
I have a column in a DataFrame called control stores:
I want to split the string with ';' delimiter and count the string. Also, in cases where there is NAN or 1 value then it should display count as 1.
I tried below code:
final_input_optimization['Control Stores'].str.split(';')[0].count('Control Stores')
But it is giving 0 as a result but should come as 6
答案1
得分: 2
final_input_optimization['Control Stores'].fillna("temp").str.split(';').map(len) 是你正在寻找的内容。
final_input_optimization['Control Stores'].fillna("temp")
将简单地用一个虚拟条目(在这种情况下是 "temp")替换所有的 NaN 值。然后,你可以像之前一样拆分 .str.split(';')
。此时,你将得到一个 pandas Series,其中每个项目都是一个列表。当你附加 .map(len)
到这个 Series 上时,它会计算每个列表的长度。
这对你有帮助吗?
英文:
I think that
final_input_optimization['Control Stores'].fillna("temp").str.split(';').map(len)
is what you are looking for.
final_input_optimization['Control Stores'].fillna("temp")
will simply replace all NaN by a dummy entry ("temp" in this case). You then split .str.split(';')
exactly like you did before. At this point you have a pandas Series where each item is a list. When you append .map(len)
on the Series it will compute the len of each list.
does it help ?
答案2
得分: 0
如果您只需要计算而不实际提取单个字符串,您可以简单地计算分隔符的出现次数:
final_input_optimization['Control Stores'].str.count(";").fillna(0).add(1)
英文:
If you just need a count without actually extracting the individual strings, you can simply count the occurrences of your delimter:
final_input_optimization['Control Stores'].str.count(";").fillna(0).add(1)
答案3
得分: 0
问题出在这里:
final_input_optimization['Control Stores'].str.split(';')[0].count('Control Stores')
str.split() 方法返回一个字符串列表。详细信息请参阅此链接描述。
在您的第二张图片中,您可以看到以下结果:
final_input_optimization['Control Stores'].str.split(';')[0]
它返回一个字符串列表。
list.count() 方法用于计算列表中给定参数的出现次数。而您提供的参数是'Control Stores',但该列表中并没有出现这个参数。要计算列表或字符串的长度,请使用内置的len() 函数,例如:
len(final_input_optimization['Control Stores'].str.split(';')[0])
或者要获取该列表中第一个字符串的长度:
len(final_input_optimization['Control Stores'].str.split(';')[0][0])
英文:
The issue is with this:
final_input_optimization['Control Stores'].str.split(';')[0].count('Control Stores')
The str.split() method returns a list of strings. See details enter link description here
In your second image you see the result of:
final_input_optimization['Control Stores'].str.split(';')[0]
being a list of strings.
The list.count() method counts the number of appearances of the given argument within the list. And your provided argument here is 'Control Stores' which is not present in that list. To count the length of a list or string, use the len() built-in, for instance:
len(final_input_optimization['Control Stores'].str.split(';')[0])
or to get the length of the first string in that list:
len(final_input_optimization['Control Stores'].str.split(';')[0][0])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论