English 中文(简体)
如何在座标丛书上适用座标
原标题:How to apply slicing on pandas Series of strings

I m playing with pandas and trying to apply string slicing on a Series of strings object. Instead of getting the strings sliced, the series gets sliced:

In [22]: s = p.Series(data=[ abcdef ]*20)
In [23]: s.apply(lambda x:x[:2])
Out[24]:
0    abcdef
1    abcdef

另一方面:

In [25]: s.apply(lambda x:x+ qwerty )
Out[25]:
0     abcdefqwerty
1     abcdefqwerty
2     abcdefqwerty
...

我用地图功能来开展工作,但我认为我不了解它如何工作。

Would very much appreciate a clarification.

最佳回答

<代码>apply 首先试图将这一功能应用于整个系列。 只有这样,才能将特定职能按每个要素划分。 <代码>[:2]是一系列的有效功能,+ qwerty ,显然指t,这说明你为什么会得到关于后者的暗中绘图。 如果你总是想做地图,你可以使用<代码>。

参考源代码:

    try:
        result = func(self)
        if not isinstance(result, Series):
            result = Series(result, index=self.index, name=self.name)
        return result
    except Exception:
        mapped = lib.map_infer(self.values, func)
        return Series(mapped, index=self.index, name=self.name)
问题回答

Wes McKinney的回答是过时的,但他对希望的潘达斯现在有有效的加固处理方法,包括:

In [2]: s = Series(data=[ abcdef ]*20)

In [3]: s.str[:2]
Out[3]:
0     ab
1     ab
2     ab
...

你回到正确的轨道上:

In [3]: s = Series(data=[ abcdef ]*20)

In [4]: s
Out[4]: 
0     abcdef
1     abcdef
2     abcdef
3     abcdef
4     abcdef
5     abcdef
6     abcdef
7     abcdef
8     abcdef
9     abcdef
10    abcdef
11    abcdef
12    abcdef
13    abcdef
14    abcdef
15    abcdef
16    abcdef
17    abcdef
18    abcdef
19    abcdef

In [5]: s.map(lambda x: x[:2])
Out[5]: 
0     ab
1     ab
2     ab
3     ab
4     ab
5     ab
6     ab
7     ab
8     ab
9     ab
10    ab
11    ab
12    ab
13    ab
14    ab
15    ab
16    ab
17    ab
18    ab
19    ab

我确实想在安达斯添加一套病媒化、无害于北美的扼杀处理工具()。 同时也感谢任何发展帮助。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签