在熊猫列内获取套件的长度

发布于 2025-02-05 07:34:08 字数 737 浏览 2 评论 0原文

我在Pandas DataFrame中的列中有一个带有字符串的集合:

                x
A  {'string1, string2, string3'}  
B  {'string4, string5, string6'}

我需要获取每组的长度,理想地创建了一个新列,结果

               x                     x_length
A  {'string1, string2, string3'}         3
B  {'string4, string5'}                  2

我不知道为什么设置为1。

这是我尝试的内容:

df['x_length'] = df['x'].str.len()

df['x_length'] = df['x'].apply(lambda x: len(x))

来自另一篇文章的自定义函数:

def to_1D(series):
 return pd.Series([len(x) for _list in series for x in _list])

to_1D(df['x'])

此功能返回整个集合中的字符数,而不是集合的长度。

我什至尝试将集合转换为列表并尝试了相同的功能,但仍然遇到了错误的结果。

我觉得我非常接近答案,但我似乎无法弄清楚。

I have a set with strings inside a column in a Pandas DataFrame:

                x
A  {'string1, string2, string3'}  
B  {'string4, string5, string6'}

I need to get the length of each set and ideally create a new column with the results

               x                     x_length
A  {'string1, string2, string3'}         3
B  {'string4, string5'}                  2

I don't know why but everything i tried to far always returns the length of the set as 1.

Here's what I've tried:

df['x_length'] = df['x'].str.len()

df['x_length'] = df['x'].apply(lambda x: len(x))

Custom function from another post:

def to_1D(series):
 return pd.Series([len(x) for _list in series for x in _list])

to_1D(df['x'])

This function returns the number of characters in the whole set, not the length of the set.

I've even tried to convert the set to a list and tried the same functions, but still got the wrong results.

I feel like I'm very close to the answer, but I can't seem to figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2025-02-12 07:34:08

我不知道为什么,但是我试图远的一切总是返回
集合的长度为1。

{'String1,String2,String3'}{'String4,String5,String6'}是固定单个str的集合(由<划分为<代码>'),而不是每个3 str设置(将是{'string1','string2','string3'} and {'string4','string string55 ',','string6'}),因此较早的某个地方存在问题,从而导致使用单个元素而不是众多。发现并消除了所述问题后,您的功能应按预期开始工作。

I don't know why but everything i tried to far always returns the
length of the set as 1.

{'string1, string2, string3'} and {'string4, string5, string6'} are sets holding single str each (delimited by ') rather than sets with 3 str each (which would be {'string1', 'string2', 'string3'} and {'string4', 'string5', 'string6'} respectively) so there is problem somewhere earlier which leads to getting sets with single element rather than multitude of them. After you find and eliminate said problem your functions should start work as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文