python - Python cannot get data from column in dataframe
问题描述
I have dataframe like this sample.
priceUsd,time,date
38492.2698958105979245,1627948800000,2021-08-03T00:00:00.000Z
39573.1543437718690816,1628035200000,2021-08-04T00:00:00.000Z
40090.5174131427618446,1628121600000,2021-08-05T00:00:00.000Z
41356.0360622010701055,1628208000000,2021-08-06T00:00:00.000Z
43535.9969201307711635,1628294400000,2021-08-07T00:00:00.000Z
I want to split last 10 rows for test dataset in tensorflow and I get data from first row to before last 10 rows for train.
train = df.loc[:-10 , ['priceUsd']]
test = df.loc[-10: , ['priceUsd']]
when I run this code it show error
TypeError: cannot do slice indexing on DatetimeIndex with these indexers [-10] of type int
How to fix it?
解决方案
试试这个:
train = df[['priceUsd']].head(len(df) - 10)
test = df[['priceUsd']].tail(10)
推荐阅读
- ruby-on-rails - 如何计算具有一个或多个关联对象的记录数?
- c# - WebRTC Xamarin Binding for iOS - 包装 Objective C 或 C++ 库?
- python - 如何查找输入数据的数据类型
- google-app-engine - Eclipse 下的 GCloud AppEngine 无法启动(Mac OS)
- javascript - JEST 抛出 .finally 不是函数
- ios - 使用 swift 4.2 为 UIView 设置动画
- c# - 如何在 C# 中将数据值从文本框扔到文本框
- c# - 如何在 Win10 UWP 中访问 SQL Server 数据库中的数据
- php - PHP:某些人通过在主体中专门抛出异常来“伪造”抽象方法的原因是什么?
- docker - 如何修复 Dockerfile CMD 错误?当我试图在 ubuntu 映像上创建一个 apache 服务器时