gpt4 book ai didi

python - 从 xlsx 导入到 Pandas 时 header 为 'None' 的列

转载 作者:行者123 更新时间:2023-12-03 17:12:24 24 4
gpt4 key购买 nike

将重格式的 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只会保留相关的列。

关于python - 从 xlsx 导入到 Pandas 时 header 为 'None' 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60439088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com