【问题标题】:Get Employee Details from the excel sheet从 excel 表中获取员工详细信息
【发布时间】:2020-11-22 19:33:17
【问题描述】:

我在 A 列中有一个仅包含“员工 ID”的 Excel 工作表,如下所示。

1677
5597
5623
5618

我还有一张包含 10000 多名员工的“员工详细信息”的 Excel 表。例如:员工详细信息 excel 表包含包含很多员工的数据这里是下面显示的员工 ID 示例之一。

Empid   Name    Location    JobTitle    Email-id     Department
1677    Umesh     Gadag      ASE      abc@gmail.com    Civil

这是工作代码

import pandas as pd
df1 = pd.read_excel (r'C:\\Users\\Kiran\\Desktop\\Employee id.xlsx',header=None)# excel sheet containing only ids
df2= pd.read_excel (r'C:\\Users\\Kiran\\Desktop\\Employee details.xlsx) # excel sheet containing all details of 10000+ employees
df3 = df2[df2['Empid'].isin(df1[0])]
df3.to_excel("Output1.xlsx",index=False)#Final output

代码工作正常,但我得到的输出是随机的

Empid   Name    Location    JobTitle    Email-id       Department
1677    Umesh     Gadag      ASE      abc@gmail.com      Civil
5623    Kiran     Hubli      SE       123@gmail.com      Civil
5618    Rudra     Bidar      ASE      xyz@gmail.com     Electrical
5597    Suresh    Udupi      ASE       ppp@gmail.com    Mechanical 

但我需要按以下顺序输出,因为员工 ID 是按特定顺序排列的

Empid   Name    Location    JobTitle    Email-id      Department
1677    Umesh     Gadag      ASE      abc@gmail.com     Civil
5597    Suresh    Udupi      ASE      ppp@gmail.com     Mechanical 
5623    Kiran     Hubli      SE       123@gmail.com     Civil
5618    Rudra     Bidar      ASE      xyz@gmail.com     Electrical

【问题讨论】:

  • 这能回答你的问题吗? Pandas Merging 101
  • 你需要存储两个不同的变量,比如df_idsdf_details
  • 你能帮帮我吗,我是 python 新手,另外我需要单独的 excel 表中的输出

标签: python pandas spyder


【解决方案1】:
from pandas import read_excel

excel_data_df = read_excel('data.xlsx', sheet_name='Sheet1')
excel_data_df.columns = ["Empid", "Name", "Location", "JobTitle", "Email-i", "Department"]


emp_id = int(input("Enter Employee id: "))
for columns in excel_data_df[excel_data_df.Empid == emp_id].values:
    for item in columns:
        print(item)

【讨论】:

  • 感谢您的代码,但是当我运行它时会出现错误“ValueError:长度不匹配:预期轴有 1 个元素,新值有 6 个元素”。我需要在单独的 excel 表中输出
【解决方案2】:

假设df_small 是包含员工ID 条目的数据框,其数据需要从df_big 获取,其中包含10000 多名员工的员工数据。

所以详细信息可以如下获取:

df_emp_details = df_big[df_big['Empid'].isin(df_small['Employee id'])]

编辑: 要读取没有标题/列名的 excel,请使用:

# This will create a default column 0 in the dataframe.
df_small = pd.read_excel('path/to/excel.xlsx', header=None)

# Use below code to fetch the details.
df_emp_details = df_big[df_big['Empid'].isin(df_small[0])]

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html


EDIT2: 我相信您希望获取的行按照员工 ID 的顺序排列。为此使用sort_values

# ...
# Sorts based on column `Empid`.
df_emp_details = df_emp_details.sort_values(by='Empid')

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html

【讨论】:

  • 代码有效,但唯一的问题是用户详细信息与员工 ID 表的顺序不同。
  • 我已经修改了整个代码,请看一下。我需要以与 Employer id excel 表中的顺序相同的方式输出,你能帮我吗
【解决方案3】:

你想要一个左连接

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.join.html

当它加入索引时,您需要确保将Empid 列设置为索引

df_small = df_small.join(df_big.set_index('Empid'), on = 'Employee ID', how = 'left')

希望该方法在未来得到改进,以便更轻松地设置要加入哪些列或不进入复杂的多索引以在多个列上加入。

【讨论】:

    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 2017-02-22
    • 2012-06-02
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多