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属性访问,通常有名称
匹配等效(标量)内置字符串方法