熊猫在这里。删除每个团队记录并将其放入新列的最佳方法是什么?提前感谢!
Rank Team
0 1 LA Rams (5-0)
1 2 New Orleans (4-1)
2 3 New England (3-2)
3 4 Kansas City (5-0)
4 5 Pittsburgh (2-2-1)
5 6 Baltimore (3-2) 发布于 2018-10-14 12:15:23
有趣的问题。
不幸的是,Series.str.extract会很容易地获取记录,但不会删除它(使用天真的正则表达式,如果团队的名称中包含(...),可以随意使用更复杂的正则表达式):
df['Record'] = df['Team'].str.extract('(\(.*?\))')
print(df)
# Rank Team record
# 0 1 LA Rams (5-0) (5-0)
# 1 2 New Orleans (4-1) (4-1)
# 2 3 New England (3-2) (3-2)
# 3 4 Kansas City (5-0) (5-0)
# 4 5 Pittsburgh (2-2-1) (2-2-1)
# 5 6 Baltimore (3-2) (3-2)这将要求我们履行自己的职能:
import re
record_regex = re.compile(r'(\(.*?\))')
records = []
def extract_and_remove_record(x):
record = record_regex.findall(x)[0]
records.append(record)
return record_regex.sub('', x)
df['Team'] = df['Team'].apply(extract_and_remove_record)
df['Record'] = records
print(df)
# Rank Team Records
# 0 1 LA Rams (5-0)
# 1 2 New Orleans (4-1)
# 2 3 New England (3-2)
# 3 4 Kansas City (5-0)
# 4 5 Pittsburgh (2-2-1)
# 5 6 Baltimore (3-2)发布于 2018-10-14 12:27:19
另一种不涉及正则表达式的方法。
df[['Team Name', 'Team Records']] = d.Team.apply(lambda x: pd.Series(x.rstrip(')').split(' (')))
df.drop('Team', axis=1, inplace=True)https://stackoverflow.com/questions/52802428
复制相似问题