【发布时间】:2021-02-23 11:46:13
【问题描述】:
假设我有一个简单的数据框,如下所示,我将此数据框的索引设置为时间列。
例如
import pandas as pd
df = pd.DataFrame({'time':['2021-02-20','2021-02-21','2021-02-22','2021-02-23'], 'price':[1,2,3,4]})
df.set_index('time', inplace=True)
现在这个数据框只有 1 个主列(价格),所以我想知道获取这个数据框的最佳方法,只需将其类型更改为系列。
我觉得这可以使用 pandas squeeze() 方法来完成,但是想知道是否有其他替代方案或更好的方法,如果我的方法看起来不对,请纠正我。
例如
# Setting the original DataFrame to a Series, since only 1 main column can use the 'column' argument
# in the squeeze method
df = df.squeeze('columns')
` ``
【问题讨论】:
-
我认为
squeeze没有问题。用法:df["price"].squeeze().
标签: python pandas dataframe series