格式化Pandas数据帧的打印输出



我正在构建一个程序来跟踪我的员工。我有一个CSV,用来跟踪信息。我正试图循环浏览并打印出没有结束日期的员工行,即那些仍在工作的员工。我已经能够打印出正确的行,但格式不是我所希望的,而是行中的。以下是我的csv:示例

csv = [employee_id,name,address,Phone,date_of_birth,job_title,start_date,end_date
1,Arya,New York,1234567890,1/1/1970,lecturer,1/1/2021,10/20/2022
2,Terri,New York,25151521,010109,Nurse,10/10/2022,
42,Bill,New York,2314,09/10/1994,Teacher,10/14/2022,
48,Steve,New York,454554,08/10/1994,Teacher,02/25/2022,
9,Stephen,New York,526415252,10/08/1994,Teacher,10/15/2022,N/A]

这是我正在运行的程序:

df2 = pd.read_csv('employees.csv')
print()
for index, row in df2.iterrows():
if ((len(str(row['end_date'])) <= 3)):
print(df2.loc[index])
else:
continue
print()

每行的打印结果如下(以下的倍数(:

employee_id             8
name                 Bill
address          New York
phone               25235
date_of_birth      081019
job_title        Engineer
start_date         081019
end_date              NaN
Name: 2, dtype: object

但是,我希望打印结果看起来像开头的csv,但只显示"end_date"列中没有值的人的行,如下所示:

[employee_id,name,address,Phone,date_of_birth,job_title,start_date,end_date
2,Terri,New York,25151521,010109,Nurse,10/10/2022,
42,Bill,New York,2314,09/10/1994,Teacher,10/14/2022,
48,Steve,New York,454554,08/10/1994,Teacher,02/25/2022,

我不想使用df.drop,因为我想把每个人都记录下来。

这应该可以工作。

import pandas as pd
import numpy as np
df = {'employee_id': {0: 1, 1: 2, 2: 42, 3: 48, 4: 9}, 'name': {0: 'Arya', 1: 'Terri', 2: 'Bill', 3: 'Steve', 4: 'Stephen'}, 'address': {0: 'New York', 1: 'New York', 2: 'New York', 3: 'New York', 4: 'New York'}, 'Phone': {0: '1234567890', 1: '25151521', 2: '2314', 3: '454554', 4: '526415252'}, 'date_of_birth': {0: '1/1/1970', 1: '010109', 2: '09/10/1994', 3: '08/10/1994', 4: '10/08/1994'}, 'job_title': {0: 'lecturer', 1: 'Nurse', 2: 'Teacher', 3: 'Teacher', 4: 'Teacher'}, 'start_date': {0: '1/1/2021', 1: '10/10/2022', 2: '10/14/2022', 3: '02/25/2022', 4: '10/15/2022'}, 'end_date': {0: '10/20/2022', 1: '', 2: '', 3: '', 4: 'N/A'}}
df['end_date'] = df['end_date'].replace(['N/A',''], np.nan)
#prints only rows with null values in end_date
df[df['end_date'].isna()]

此外,还有一种更简单的方法可以为每位员工获得垂直打印输出:

for e in df['employee_id']:
df[df['employee_id']==e].transpose()

#output
employee_id             1
name                 Arya
address          New York
Phone          1234567890
date_of_birth    1/1/1970
job_title        lecturer
start_date       1/1/2021
end_date       10/20/2022

相关内容

  • 没有找到相关文章

最新更新