我无法弄清楚python pandas中是否有语句。
我有一个数据帧df
Category | Count
A 45756
B 5857
C 57876
我创建一个列表,然后将其用作层次结构
list_s = {'A':'Y',
'B':'Y'}
df['Flag'] = df['Category'].replace(list_s)
但我得到
Category | Count | Flag
A 45756 Y
B 5857 Y
C 57876 C
而不是
Category | Count | Flag
A 45756 Y
B 5857 Y
C 57876
谢谢!
答案 0 :(得分:2)
将lambda
与get
函数一起使用,您可以使用字典list_s = {'A':'Y', 'B':'Y'}
df['Flag'] = df['Category'].map(lambda x: list_s.get(x, ''))
print(df)
Category Count Flag
0 A 45756 Y
1 B 5857 Y
2 C 57876
方法传递默认值
@Controller
public class ApplicantApplicationsListController extends ApplicantController {
/**
* @throws Exception
*
*/
@RequestMapping(value = { "/medrano/applicant/home",
"/medrano/applicant/home/"}, method = {RequestMethod.GET})
public String viewProductApplications (@ModelAttribute("applicationApplicationsListForm") final ApplicationApplicationsListForm applicationApplicationsListForm,
HttpServletRequest request,
Model model ) throws Exception {
return "applicantApplicationsView";
}
答案 1 :(得分:2)
IIUC你可以这样做:
In [8]: df['Flag'] = df['Category'].map(list_s).fillna('')
In [9]: df
Out[9]:
Category Count Flag
0 A 45756 Y
1 B 5857 Y
2 C 57876