我正在尝试创建一个新列,并使用每行中的值填充它。我有一个专栏'Journey',新专栏是'Origin'。
def getOrigin(journey):
if " to " in journey:
return journey.split(" to ")[0]
else:
return "No origin"
df['Origin'] = getOrigin(df.Journey)
print(df['Origin'])
如果df.Journey是“America to England”,那么我希望df ['Origin']是'America',但是每一行Origin都是“No origin”。我该怎么做?
答案 0 :(得分:1)
我相信你需要像这样映射:
df['Origin'] = df.Journey.applymap(getOrigin)
这应该将您的功能应用于Journey列中的每个项目
答案 1 :(得分:1)
这个解决方案效率较低,代码更多,但作为初学者,更容易理解...... 与您尝试解决问题的方式一致......!
df = pd.DataFrame(data = {'Journey' : ['england to america', 'peru', 'france to china']})
origin = []
def getOrigin(Journey):
for i in range(len(Journey)):
if " to " in Journey[i]:
origin.append(Journey[i].split(" to ")[0])
else:
origin.append("No origin")
return origin
df['Origin'] = getOrigin(df['Journey'])
print (df['Origin'])
0 england
1 No origin
2 france
Name: Origin, dtype: object
答案 2 :(得分:0)
str.extract
+ fillna
df['Origin'] = df['Journey'].str.extract('^(.*?)(?=\s*to)').fillna('No origin')
str.split
+ fillna
df['Origin'] = df['Journey'].str.split(' to').str[0].fillna('No origin')
df['Origin'] = [
x.split(' to ')[0] if 'to' in x else 'No origin' for x in df['Journey']
]