将列中的值用作另一列pandas的字符串切片机

发布于 2025-02-13 16:18:33 字数 891 浏览 2 评论 0原文

我正在尝试将单元格值用作新列中字符串的切片。例如,如果我创建此表。

data = pd.DataFrame(data = {'Name':['This is a title'], 'Number':[-5]})

               Name Number
0   This is a title     -5

并创建一个类似的新列:

data['Test'] = data.Name.str[:data.Number.item()]

它将创建新的列,如预期:

               Name Number       Test
0   This is a title     -5  This is a 

当我的行超过行时发生问题,因此,如果我创建下表:

 data = pd.DataFrame(data = {'Name':['This is a title', 'This is another title'], 'Number':[-5, -13]})

                     Name   Number
0   This is a title             -5
1   This is another title      -13

“测试”列的创建产生:

can only convert an array of size 1 to a Python scalar

我理解为什么这是因为该列现在具有多个值以来,我想知道的是如何使用具有多个行以上的数据框架来执行此操作?我尝试了.items(),.values()等。新列只是NAN。

有什么想法吗?

谢谢!

I'm trying to use a cell value as the slice for a string in a new column. For example, if I create this table.

data = pd.DataFrame(data = {'Name':['This is a title'], 'Number':[-5]})

               Name Number
0   This is a title     -5

And create a new column like so:

data['Test'] = data.Name.str[:data.Number.item()]

It'll create the new column, as expected:

               Name Number       Test
0   This is a title     -5  This is a 

The issue occurs when I have more than row, so if I create the following table:

 data = pd.DataFrame(data = {'Name':['This is a title', 'This is another title'], 'Number':[-5, -13]})

                     Name   Number
0   This is a title             -5
1   This is another title      -13

The creation of the 'Test' column yields:

can only convert an array of size 1 to a Python scalar

I understand why this is happening since the column now has more than one value, what I want to know is how can I do this with a dataframe that has more than one row? I've tried .items(), .values(), etc. and the new column just becomes NaN.

Any thoughts?

Thanks!

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

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

发布评论

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

评论(2

北渚 2025-02-20 16:18:33

您可以使用使用axis = 1使用,然后按dataframe行进。

import pandas as pd
data = pd.DataFrame(data = {'Name':['This is a title', 'This is another title'], 'Number':[-5, -13]})

data['Test'] = data.apply(lambda row: row['Name'][:row['Number']], axis=1)
print(data)

输出:

                    Name  Number        Test
0        This is a title      -5  This is a 
1  This is another title     -13    This is 

You can use apply with axis=1 and move on dataframe row by row.

import pandas as pd
data = pd.DataFrame(data = {'Name':['This is a title', 'This is another title'], 'Number':[-5, -13]})

data['Test'] = data.apply(lambda row: row['Name'][:row['Number']], axis=1)
print(data)

Output:

                    Name  Number        Test
0        This is a title      -5  This is a 
1  This is another title     -13    This is 
遗弃M 2025-02-20 16:18:33

不幸的是,在这里,您需要循环。列表理解将是最有效的:

data['Test'] = [s[:i] for s,i in zip(data['Name'], data['Number'])]

输出:

                    Name  Number        Test
0        This is a title      -5  This is a 
1  This is another title     -13    This is 

Unfortunately, here, you need to loop. A list comprehension will be the most efficient:

data['Test'] = [s[:i] for s,i in zip(data['Name'], data['Number'])]

output:

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