Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 找出pd.系列的长度并去掉最后两个字符_Python_Python 3.x_Pandas_Dataframe_Series - Fatal编程技术网

Python 找出pd.系列的长度并去掉最后两个字符

Python 找出pd.系列的长度并去掉最后两个字符,python,python-3.x,pandas,dataframe,series,Python,Python 3.x,Pandas,Dataframe,Series,我知道我可以通过使用pd.Series.str.len()找到pd.Series的长度,但是有没有办法去掉最后两个字符?我知道我们可以使用Python来实现这一点,但我很好奇是否可以在熊猫身上实现 例如: $1000.0000 1..0009 456.2233 结果将是: $1000.00 1..00 456.22 如果您有任何见解,我们将不胜感激。请尝试 df_new = df.astype(str).applymap(lambda x :

我知道我可以通过使用
pd.Series.str.len()
找到pd.Series的长度,但是有没有办法去掉最后两个字符?我知道我们可以使用Python来实现这一点,但我很好奇是否可以在熊猫身上实现

例如:

    $1000.0000
    1..0009
    456.2233
结果将是:

    $1000.00
    1..00
    456.22
如果您有任何见解,我们将不胜感激。

请尝试

df_new = df.astype(str).applymap(lambda x : x[:-2])
或者只有一列

df_new = df.astype(str).str[:-2]
只要做:

import pandas as pd

s = pd.Series(['$1000.0000', '1..0009', '456.2233'])
res = s.str[:-2]
print(res)
输出

0    $1000.00
1       1..00
2      456.22
dtype: object
Pandas通过访问器str支持内置字符串方法,来自:

它们通过str属性访问,通常有名称 匹配等效(标量)内置字符串方法