将格式繁琐的excel工作表导入熊猫会导致某些列完全空白,并且在查看df.columns
时显示为“无”。我需要删除这些列,但是我得到一些奇怪的输出,这使我很难弄清楚如何精确删除它们。
****为了清晰起见进行编辑****
excel工作表的格式已被严格格式化,必须重新调整其格式以供分析中使用的数据使用。本质上,列A是问题列表,列B是每个问题的解释,列C是对问题的回答。理想的结果是col A成为表格数据集的标题,col B被删除,而col C是第一行。然后需要以一种方式保存该数据,以便可以将excel工作表的另一个副本(将为另一个客户端填写)的col C附加到表格数据集。
我已经能够将工作表导入python和pandas中,转置数据,并进行一些最小的重塑和清理。
示例代码:
import os
import pandas as pd
import xlwings as xw
dir_path = "C:\\Users\\user.name\\directory\\project\\data\\january"
file_path = "C:\\Users\\user.name\\directory\\project\\data\\january\\D10A0021_10.01.20.xlsx"
os.chdir(dir_path)# setting the directory
wb = xw.Book(file_path, password = 'mypassword') # getting python to open the workbook
demographics = wb.sheets[0] # selecting the demographic sheet.
df = demographics['B2:D33'].options(pd.DataFrame, index=False, header = True).value # importing all the used cells into pandas
df.columns = [0,1,2] #adding column names that I can track
df = df.T #Transposing the data
df.columns = df.loc[0] #turning the question items into the column headers
df = df.loc[2:] remove the unneeded first and second row from the set
for num, col in enumerate(df.columns):
print(f'{num}: {col}') # This code has fixed the issue one of the issues. Suggested by Datanovice.
Output:
0: Client code
1: Client's date of birth
2: Sex
3: Previous symptom recurrence
4: None
5: Has the client attended Primary Care Psychology in the past?
6: None
7: Ethnicity
8: None
9: Did the parent/ guardian/ carer require help completing the scales due to literacy difficulties?
10: Did the parent/ guardian/ carer require help completing the scales due to perceived complexity of questionnaires?
11: Did the client require help completing the scales due to literacy difficulties?
12: Did the client require help completing the scales due to perceived complexity of questionnaires?
13: Accommodation status
14: None
15: Relationship with main carer
16: None
17: Any long term stressors
18: Referral source
19: Referral date
20: Referral reason
21: Actual presenting difficulty (post formulation)
22: Date first seen
23: Discharge date
24: Reason for terminating treatment
25: None
26: Type of intervention
27: Total number of sessions offered (including DNA’s CNA’s)
28: No. of sessions: attended (by type of intervention)
29: No. of sessions: did not attend (by type of intervention)
30: No. of sessions: could not attend (by type of intervention)
31
在将数据导出到另一个excel工作表之前,我需要能够删除标题中具有“无”的任何列,然后在提交新的客户记录时可以使用新数据更新该工作表。
任何建议将不胜感激。
答案 0 :(得分:1)
因此,您有一个 Excel 工作表,其中有些列没有数据。
并且xlwings
会将所有没有数据的单元格默认设置为NaN
/ None
。
您可以做的是,仅将名称不为None
的列保留为:
cols = [x for x in df.columns if x is not None]
df = df[cols]
然后df
仅保留相关列。