简体   繁体   English

熊猫数据框和系列

[英]Pandas data frame and series

The pandas data frame is of 30 rows after reading the excel file. 读取excel文件后,熊猫数据框为30行。 It filters to one row (it will always be one row after a filter), how can i retain the data frame as data frame. 它过滤到一行(过滤器之后总是一行),我如何将数据帧保留为数据帧。 After the filter (deletion of rows) it converts the data frame to series. 在过滤器(删除行)之后,它将数据帧转换为序列。

The excel file ppfileloc has 30 rows for the month of april and for a selected date it will always be one record in the data frame. excel文件ppfileloc在4月月份有30行,在选定的日期,它将始终是数据框中的一条记录。 The resulting data frame reshapes to series. 结果数据框将重塑为系列。

#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

How can I convert df3 to two columns with planet and degrees as columns titles? 如何将df3转换为以行星和度为标题的两列?

If you want to convert a Series to a DataFrame , you can just do series.to_frame() . 如果要将Series转换为DataFrame ,只需执行series.to_frame() This accepts a name argument to say what to name single column of the dataframe. 这接受一个name参数来说明要为数据框的单个列命名的内容。 This will leave the index as an index. 这样会将索引保留为索引。 Since you want it as a column instead, you have to reset the index as well and then rename it as you want. 由于您希望将其作为一列,因此您还必须重置索引,然后根据需要对其重命名。

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

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

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