[英]Create new column based on value of another column
I have a solution below to give me a new column as a universal identifier, but what if there is additional data in the NAME column, how can I tweak the below to account for a wildcard like search term?我在下面有一个解决方案,可以给我一个新列作为通用标识符,但是如果 NAME 列中有其他数据怎么办,我如何调整下面的内容以说明像搜索词这样的通配符?
I want to basically have so if German/german or Mexican/mexican is in that row value then to give me Euro or South American value in new col我基本上想要如果德国/德国或墨西哥/墨西哥在该行值中,那么在新列中给我欧元或南美价值
df["Identifier"] = (df["NAME"].str.lower().replace(
to_replace = ['german', 'mexican'],
value = ['Euro', 'South American']
))
print(df)
NAME Identifier
0 German Euro
1 german Euro
2 Mexican South American
3 mexican South American
Desired output
NAME Identifier
0 1990 German Euro
1 german 1998 Euro
2 country Mexican South American
3 mexican city 2006 South American
Based on an answer in this post :基于这篇文章中的答案:
r = '(german|mexican)'
c = dict(german='Euro', mexican='South American')
df['Identifier'] = df['NAME'].str.lower().str.extract(r, expand=False).map(c)
Another approach would be using np.where
with those two conditions, but probably there is a more ellegant solution.另一种方法是在这两个条件下使用
np.where
,但可能有更优雅的解决方案。
below code will work.下面的代码将起作用。 i tried it using apply function but somehow can't able to get it.
我尝试使用 apply function 但不知何故无法获得它。 probably in sometime.
可能在某个时候。 meanwhile workable code below
同时下面的可行代码
df3['identifier']=''
js_ref=[{'german':'Euro'},{'mexican':'South American'}]
for i in range(len(df3)):
for l in js_ref:
for k,v in l.items():
if k.lower() in df3.name[i].lower():
df3.identifier[i]=v
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.