使用子字符串和if-elif条件创建新列

嗨,你们好,
我刚接触巨蟒,所以请善待我。
以下是数据框和要求:

选择 | 换行 | 行号
  1. data = {'id': ['aa11bc', 'bb22cd', 'cc33ef', 'dd44gh', 'ee55ij','ff66kl','gg77mn','hh88op'], 
  2.         'direction': ["north, south, east, west", "north, south, east, west", "north, south/, *east, \west%", "north, south, east, west",
  3.                       "north, south, east, west","north, south, east, west","north, south, east, west"
  4.                      ,"north, south, east, west"]}
  5. df = pd.DataFrame(data, columns = ['id','direction'])
  6. df
  7.  
  8. Requirement:
  9. #if id (2nd and 3rd letter) in ('a1','b2') then new_col should have north as an observation from direction column
  10. #else if id (2nd and 3rd letter) in ('c3','d4') then new_col should have south as an observation from direction column
  11. #else if id (2nd and 3rd letter) in ('e5','f6') then new_col should have east as an observation from direction column
  12. #else if id (2nd and 3rd letter) in ('g7','h8') then new_col should have west as an observation from direction column

我尝试了下面的代码:但输出不正确,它没有满足所需的输出。

选择 | 换行 | 行号
  1. new_col=[]
  2.  
  3. for i in df["id"]:
  4.     if i[1:3].lower()in ('a1','b2'):
  5.         new_col=df["direction"].str.split(',').str[0].str.replace('\W+',' ').str.strip()
  6.     elif i[1:3].lower()in ('c3','d4'):
  7.         new_col=df["direction"].str.split(',').str[1].str.replace('\W+',' ').str.strip()
  8.     elif i[1:3].lower()in ('e5','f6'):
  9.         new_col=df["direction"].str.split(',').str[2].str.replace('\W+',' ').str.strip()
  10.     elif i[1:3].lower()in ('g7','h8'):
  11.         new_col=df["direction"].str.split(',').str[3].str.replace('\W+',' ').str.strip()
  12.  
  13. df["position"]=new_col
  14. print(df)
  15.  
  16. And the output as follows:
  17.        id                     direction position
  18. 0  aa11bc      north, south, east, west     west
  19. 1  bb22cd      north, south, east, west     west
  20. 2  cc33ef  north, south/, *east, \west%     west
  21. 3  dd44gh      north, south, east, west     west
  22. 4  ee55ij      north, south, east, west     west
  23. 5  ff66kl      north, south, east, west     west
  24. 6  gg77mn      north, south, east, west     west
  25. 7  hh88op      north, south, east, west     west

请指点一下。

# 回答1


我不明白你想做什么,但可以吗?
您是否需要替换方向上的非字母?

选择 | 换行 | 行号
  1. new_col=[]
  2. j = 0
  3. for i in df["id"]:
  4.     if i[1:3].lower()in ('a1','b2'):
  5.         new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[0]))
  6.     elif i[1:3].lower()in ('c3','d4'):
  7.         new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[1]))
  8.     elif i[1:3].lower()in ('e5','f6'):
  9.         new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[2]))
  10.     elif i[1:3].lower()in ('g7','h8'):
  11.         new_col.append(re.sub(r'\W+','',df.loc[j, 'direction'].split(',')[3]))
  12.     j = j + 1
  13. df ["position"] = new_col
  14. print(df)
  15.  
# 回答2


嗨,你们好,
谢谢你的帮助。
是的,我正在从您的代码中获得所需的输出。
我想从你的代码中再试一个条件,我会让你知道状态。在此之前,我将保持这个问题的悬而未决。
希望你对此没意见。
再次感谢您的帮助。
干杯!!
# 回答3


嗨,Siosio,
感谢您对代码的帮助。

标签: python

添加新评论