从 excel sheet 获取员工详细信息
Get Employee Details from the excel sheet
我有一个 excel sheet 在 A 列中仅包含“员工 ID”,如下所示。
1677
5597
5623
5618
我还有一个 excel sheet 包含 10000 多名员工的“员工详细信息”。例如:员工详细信息 excel sheet 包含包含大量员工的数据,这里是下面显示的员工 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
但是我需要按如下顺序输出,因为 Employee 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
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)
假设 df_small
是包含员工 ID 条目的数据框,其数据需要从 df_big
中获取,其中包含 10000 多名员工的员工数据。
所以详情可以如下获取:
df_emp_details = df_big[df_big['Empid'].isin(df_small['Employee id'])]
编辑:
要在没有 header/column 名字的情况下阅读 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
编辑2:
我相信您希望获取的行按照员工 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
您想要左连接
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')
希望该方法在未来得到改进,以便更轻松地设置要连接的列或不进入复杂的多索引以连接多个列。
我有一个 excel sheet 在 A 列中仅包含“员工 ID”,如下所示。
1677
5597
5623
5618
我还有一个 excel sheet 包含 10000 多名员工的“员工详细信息”。例如:员工详细信息 excel sheet 包含包含大量员工的数据,这里是下面显示的员工 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
但是我需要按如下顺序输出,因为 Employee 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
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)
假设 df_small
是包含员工 ID 条目的数据框,其数据需要从 df_big
中获取,其中包含 10000 多名员工的员工数据。
所以详情可以如下获取:
df_emp_details = df_big[df_big['Empid'].isin(df_small['Employee id'])]
编辑: 要在没有 header/column 名字的情况下阅读 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
编辑2:
我相信您希望获取的行按照员工 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
您想要左连接
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')
希望该方法在未来得到改进,以便更轻松地设置要连接的列或不进入复杂的多索引以连接多个列。