[英]Pandas delete parts of string after specified character inside a dataframe
I would like a simple mehtod to delete parts of a string after a specified character inside a dataframe. 我想要一个简单的方法来删除数据帧内指定字符后的字符串部分。 Here is a simplified example: 这是一个简化的例子:
df: DF:
obs a b c d
0 1 1-23-12 1 2 3
1 2 12-23-13 4 5 5
2 3 21-23-14 4 5 5
I would like to remove the parts in the a column after the first - sign, my expected output is: 我想在第一个符号后删除a列中的部分,我的预期输出是:
newdf: newdf:
obs a b c d
0 1 1 1 2 3
1 2 12 4 5 5
2 3 21 4 5 5
You can reformat the values by passing a reformatting function into the apply
method as follows: 您可以通过将重新格式化函数传递给apply
方法来重新格式化值,如下所示:
from StringIO import StringIO
import pandas as pd
data = """ obs a b c d
1 1-23-12 1 2 3
2 12-23-13 4 5 5
3 21-23-14 4 5 5"""
# Build dataframe from data
df = pd.read_table(StringIO(data), sep=' ')
# Reformat values for column a using an unnamed lambda function
df['a'] = df['a'].apply(lambda x: x.split('-')[0])
This gives you your desired result: 这可以为您提供所需的结果:
obs a b c d
0 1 1 1 2 3
1 2 12 4 5 5
2 3 21 4 5 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.