我需要从另一列创建一个新列。 数据集是由以下代码创建的(我只提取了几行):
import pandas as pd
new_dataframe = pd.DataFrame({
"Name": ['John', 'Lukas', 'Bridget', 'Carol','Madison'],
"Notes": ["__ years old. NA", "__ years old. NA",
"__ years old. NA", "__ years old. Old account.",
"__ years old. New VIP account."],
"Status": [True, False, True, True, True]})
这会产生以下结果
Name Notes Status
John 23 years old. NA True
Lukas 52 years old. NA False
Bridget 64 years old. NA True
Carol 31 years old. Old account True
Madison 54 years old. New VIP account. True
我需要创建两个新列,其中包含以下格式的年龄信息:
到最后我应该
Name Notes Status L_Age S_Age
John 23 years old. NA True 23 years old 23
Lukas 52 years old. NA False 52 years old 52
Bridget 64 years old. NA True 64 years old 64
Carol 31 years old. Old account True 31 years old 31
Madison 54 years old. New VIP account. True 54 years old 54
我不知道如何提取前三个单词,然后只提取第一个,以创建新的列。我试过了
new_dataframe.loc[new_dataframe.Notes == '', 'L_Age'] = new_dataframe.Notes.str.split()[:3]
new_dataframe.loc[new_dataframe.Notes == '', 'S_Age'] = new_dataframe.Notes.str.split()[0]
但这是错误的
我们将不胜感激
您可以使用此模式提取信息并加入:
输出:
IIUC:
相关问题 更多 >
编程相关推荐