Python 尝试使用Pandas数据帧的前23行作为标头,然后围绕标头旋转
我正在使用tabla拉入数据框。不幸的是,数据按行排列,如下所示。我需要获取前23行,并将它们用作剩余数据的列标题。我需要每行包含这23个标题,每个标题对应大约60个诊所Python 尝试使用Pandas数据帧的前23行作为标头,然后围绕标头旋转,python,pandas,dataframe,Python,Pandas,Dataframe,我正在使用tabla拉入数据框。不幸的是,数据按行排列,如下所示。我需要获取前23行,并将它们用作剩余数据的列标题。我需要每行包含这23个标题,每个标题对应大约60个诊所 Col \ 0 Date 1 Clinic 2
Col \
0 Date
1 Clinic
2 Location
3 Clinic Manager
4 Lease Cost
5 Square Footage
6 Lease Expiration
8 Care Provided
9 # of Providers (Full Time)
10 # FTE's Providing Care
11 # Providers (Part-Time)
12 Patients seen per week
13 Number of patients in rooms per provider
14 Number of patients in waiting room
15 # Exam Rooms
16 Procedure rooms
17 Other rooms
18 Specify other
20 Other data:
21 TI Needs:
23 Conclusion & Recommendation
24 Date
25 Clinic
26 Location
27 Clinic Manager
28 Lease Cost
29 Square Footage
30 Lease Expiration
32 Care Provided
33 # of Providers (Full Time)
34 # FTE's Providing Care
35 # Providers (Part-Time)
36 Patients seen per week
37 Number of patients in rooms per provider
38 Number of patients in waiting room
39 # Exam Rooms
40 Procedure rooms
41 Other rooms
42 Specify other
44 Other data:
45 TI Needs:
47 Conclusion & Recommendation
Val
0 9/13/2017
1 Gray Medical Center
2 1234 E. 164th Ave Thornton CA 12345
3 Jane Doe
4 $23,074.80 Rent, $5,392.88 CAM
5 9,840
6 7/31/2023
8 Family Medicine
9 12
10 14
11 1
12 750
13 4
14 2
15 31
16 1
17 X-Ray, Phlebotomist/blood draw
18 NaN
20 Facilities assistance needed. 50% of business...
21 Paint and Carpet (flooring is in good conditio...
23 Lay out and occupancy flow are good for this p...
24 9/13/2017
25 Main Cardiology
26 12000 Wall St Suite 13 Main CA 12345
27 John Doe
28 $9610.42 Rent, $2,937.33 CAM
29 4,406
30 5/31/2024
32 Cardiology
33 2
34 11, 2 - P.T.
35 2
36 188
37 0
38 2
39 6
40 0
41 1 - Pacemaker, 1 - Treadmill, 1- Echo, 1 - Ech...
42 Nurse Office, MA station, Reading Room, 2 Phys...
44 Occupied in Emerus building. Needs facilities ...
45 New build out, great condition.
47 Practice recently relocated from 84th and Alco...
通过修复标题,我能够将数据帧放在更好的位置。我将重新发布前3组数据,以更好地说明数据框架的结构。所有内容都会重复每个诊所的标题和值。尝试以下操作:
df2 = pd.DataFrame(df[23:].values.reshape(-1, 23),
columns=df[:23][0])
print(df2)
理想情况下,数字23是结果df的每行中的列数。您可以将其替换为所需的列数 df=df.T;df=df.set_indexdf.columns[:23].T?感谢您的快速回复。当我运行您的代码时,我遇到了以下错误:ValueError:长度不匹配:预期为2行,接收长度为23的数组是否有其他人可以对此进行权衡或需要进一步澄清?每个数据是否在其自己的帧中?你确定每个数据的长度完全相同吗?我不确定。我知道现在,Date作为索引被引入,它真的应该只是其中的一列。一旦解决了这个问题,我有大约60列与上面第二组相同的内容,需要转换为每个诊所一行。第一个分组将用作标题,如果您添加日期,我希望最后有60多行24列。谢谢,David。我得到了以下错误:ValueError:无法将大小为2640的数组重塑为形状23我只是重新发布了更多的输出,以帮助说明数据帧的当前结构。我猜您缺少几行。您尝试重塑的行数必须是23的倍数。根据错误消息,您有2640行,不能被23整除。我假设您在这里缺少一行,因为2641可以被23整除。你能重新验证你的数据集吗?对不起,是的,我已经从数据框中删除了空行。我现在总共有1176行,每组21行,总共56个组。因此,这似乎在目前起作用,很大程度上是由于您的输入。非常感谢。它并不漂亮,但在这里是:df=df.dropnaaxis=0,how='all'df=df.reset_indexdrop=True df.columns=['Col','Val']headers=df[0:21].T.iloc[0]df3=df.Val df3=pd.DataFramedf3.values.reforme-1,21,columns=headers