我想打印一个热编码数据的前5个和后5个。代码如下。打印时,将打印第一个和最后30个。
Code:
from random import randint
import pandas_datareader.data as web
import pandas as pd
import datetime
import itertools as it
import numpy as np
import csv
df = pd.read_csv('C:Users\GrahamFam\Desktop\Data Archive\Daily3mid(Archive).txt')
df.columns = ['Date','b1','b2','b3']
df = df.set_index('Date')
reversed_df = df.iloc[::-1]
n=5
#print(reversed_df.drop(df.index[n:-n]))
df = pd.read_csv('C:Users\GrahamFam\Desktop\Data Archive\Daily3eve(Archive).txt')
df.columns = ['Date','b1','b2','b3']
df = df.set_index('Date')
reversed_df = df.iloc[::-1]
n=5
print(reversed_df.drop(df.index[n:-n]),("\n"))
BallOne = pd.get_dummies(reversed_df.b1)
BallTwo = pd.get_dummies(reversed_df.b2)
BallThree = pd.get_dummies(reversed_df.b3)
print(BallOne)
print(BallTwo)
print(BallThree)
答案 0 :(得分:2)
答案 1 :(得分:0)
如果可以在头部前显示尾巴,那么可以使用np.r_
从负向正切片:
import pandas as pd
import numpy as np
df = pd.DataFrame(list(range(30)))
df.iloc[np.r_[-3:3]]
# 0
#27 27
#28 28
#29 29
#0 0
#1 1
#2 2
否则,请明确切片:
n = 3
l = len(df)
df.iloc[np.r_[0:n, l-n:l]]
# 0
#0 0
#1 1
#2 2
#27 27
#28 28
#29 29