繁体   English   中英

迭代 dataframe - pandas 中的列值

[英]Iterate over column values in a dataframe - pandas

我有一张这样的桌子

emp_id emp_role mgr_id 经理角色
111 美联社 112 SP
112 SP 116 DP
114 唱片 115 DP

对于每个员工,我需要打印 emp_role、他的 mgr_id 和 mgr_role

我试过这个

for id in df['emp_id']:
    print(id + 'with role' + df['emp_role'] + 'is reporting to' + df['mgr_id'] + 'with role' + df['mgr_role']

这会多次打印 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

我认为你真的很接近你的方法。 根据您在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM