数据帧和序列

2024-10-03 19:29:59 发布

您现在位置:Python中文网/ 问答频道 /正文

读取excel文件后,数据框为30行。它过滤到一行(过滤后总是一行),我怎样才能保留数据帧作为数据帧。在筛选(删除行)之后,它将数据帧转换为序列

excel文件ppfileloc在4月份有30行,对于选定的日期,它将始终是数据框中的一条记录。生成的数据帧将重塑为序列

#Following is the code and output in jupyter. 

df = pd.read_excel(ppfileloc)
df.set_index('Date',inplace=True)
date = 15
df1 = df.loc[date,:]
df2 = pd.DataFrame(df1)
df3 = df2.drop(labels=['Day', 'Sunrise', 'Sunset   ', 'SidTime    '])

df3.shape
# (8, 1)

#df3 command fetches the following as output
df3  

#         15
# Sun       000:37:56
# Moon      119:42:25
# Mars      045:36:33
# Mercury   333:14:41
# Jupiter   240:11:57
# Venus     329:01:24
# Saturn    266:12:49
# Rahu      087:52:24

如何将df3转换为以行星和度数作为列标题的两列


Tags: 文件the数据dfoutputdate序列excel
1条回答
网友
1楼 · 发布于 2024-10-03 19:29:59

如果要将Series转换为DataFrame,只需执行series.to_frame()。它接受一个name参数来说明如何命名数据帧的单个列。这将保留索引作为索引。因为您希望它作为一个列,所以还必须重置索引,然后根据需要重命名它

df3.to_frame(name="degrees").reset_index().rename(columns={"index": "planet"})

相关问题 更多 >