繁体   English   中英

从 xlsx 导入到 Pandas 时带有“无”标题的列

[英]Columns with 'None' header when importing from xlsx to pandas

将重格式的 excel 工作表导入到 Pandas 中会导致某些列在查看df.columns时完全空白且为“无”。 我需要删除这些列,但我得到了一些奇怪的输出,这让我很难弄清楚如何准确地删除它们。

****为清晰起见进行编辑****

excel 工作表的格式很重,必须重新调整形状才能用于分析中的数据。 本质上,col A 是问题列表,col B 是对每个问题的解释,col 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 工作表之前,我需要能够删除标题中包含“无”的任何列,然后可以在提交新客户记录时使用新数据进行更新。

任何建议将不胜感激。

所以你有一个Excel 工作表,其中有一些没有数据的列。 默认情况下, xlwings会将所有没有数据的单元格设置为NaN / None

您可以做的是只保留名称不为None列:

cols = [x for x in df.columns if x is not None]
df = df[cols]

然后df将只保留相关列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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