我正在使用Pandas处理列表数据。列表数据是
.object-fitimg
我创建标签并使用DataFrame.from_records读取数据
[(datetime.date(1992, 2, 1), 114535.0), (datetime.date(1992, 3, 1), 120025.0), (datetime.date(1992, 4, 1), 124470.0), (datetime.date(1992, 5, 1), 125822.0), (datetime.date(1992, 6, 1), 122834.0)]
我使用labels = ['date', 'value']
df = pd.DataFrame.from_records(listData, columns=labels)
df = df.set_index('date')
print(df.loc['1992-03-01'])
df.loc
答案 0 :(得分:2)
使用to_datetime
创建DatetimeIndex
,将dates objects
转换为timestamp
:
labels = ['date', 'value']
df = pd.DataFrame.from_records(listData, columns=labels)
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
print(df.loc['1992-03-01'])
value 120025.0
Name: 1992-03-01 00:00:00, dtype: float64
或按日期对象查看:
labels = ['date', 'value']
df = pd.DataFrame.from_records(listData, columns=labels)
df = df.set_index('date')
print(df.loc[datetime.date(1992, 3, 1)])
value 120025.0
Name: 1992-03-01, dtype: float64