首页 > 解决方案 > 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?

标签: pythondataframe

解决方案


试试这个:

train = df[['priceUsd']].head(len(df) - 10)
test = df[['priceUsd']].tail(10)

推荐阅读