[英]Iterate over column values in a dataframe - pandas
I have a table like this我有一张这样的桌子
emp_id emp_id | emp_role emp_role | mgr_id mgr_id | mgr_role经理角色 |
---|---|---|---|
111 111 | AP美联社 | 112 112 | SP SP |
112 112 | SP SP | 116 116 | DP DP |
114 114 | LP唱片 | 115 115 | DP DP |
For each employee, I need to print the emp_role, his mgr_id and mgr_role对于每个员工,我需要打印 emp_role、他的 mgr_id 和 mgr_role
I have tried this我试过这个
for id in df['emp_id']:
print(id + 'with role' + df['emp_role'] + 'is reporting to' + df['mgr_id'] + 'with role' + df['mgr_role']
This prints the output multiple times but i need to print it exactly once.这会多次打印 output 但我只需要打印一次。 Please help to resolve this.请帮助解决这个问题。 Thanks in advance.提前致谢。
Expected output:预计 output:
111 with role AP is reporting to 112 with role SP
112 with role SP is reporting to 116 with role DP
114 with role LP is reporting to 115 with role DP
I think that you are really close to your approach.我认为你真的很接近你的方法。 Depending on how you defined your table in Python, you can use print with format.根据您在 Python 中定义表格的方式,您可以使用带有格式的打印。
table = [ [111, "AP", 112, "SP"],
[112, "SP", 116, "DP"],
[114, "LP", 115, "DP"]
]
for row in table:
emp_id, emp_role, mgr_id, mgr_role = row
print("{} with role {} is reporting to {} with role {}".format(emp_id, emp_role, mgr_id, mgr_role))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.