@sidonduarte/

pandas-head-tail

Python

Tail and Head in pandas

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.5035605619675487","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"IyMKIyBCeSBTaWRvbgojIFJlc3Bvc3RhIG5vIFNUT3B0ICAKIyBodHRwczovL3B0LnN0YWNrb3ZlcmZsb3cuY29tL2EvMzY3NjYyLzc0MTIKIyMKCmltcG9ydCBwYW5kYXMgYXMgcGQKCmRhZG9zID0gW1snMjI4Mjg5NDknLCAnMjAxOS0wMi0yNicsICc0NTMuMDAnXSwKIFsnMjI4Mjg5NDknLCAnMjAxOS0wMi0yMicsICc0NTMuMDAnXSwKIFsnMjI4Mjg5NDknLCAnMjAxOS0wMi0xOCcsICc0NTMuMDAnXSwKIFsnMjI4Mjg5NDknLCAnMjAxOS0wMi0wNScsICc0NTMuMDAnXSwKIFsnMjI4Mjg5NTAnLCAnMjAxOS0wMi0yNicsICcxODkuMDAnXSwKIFsnMjI4Mjg5NTAnLCAnMjAxOS0wMi0yNCcsICcxODkuMDAnXV0KCiMgQ29uc3RydWluZG8gbyBkYXRhZnJhbWUKZGYwID0gcGQuRGF0YUZyYW1lKGRhZG9zLCBjb2x1bW5zPVsnSUQnLCAnRFRfREFURScsICdOUl9QUklDRSddKS5zb3J0X3ZhbHVlcyhieT1bJ0RUX0RBVEUnXSkKCiMgUmVtb3ZlbmRvIGFzIGRhdGFzIGR1cGxpY2F0YXMKZGYxID0gZGYwLmRyb3BfZHVwbGljYXRlcyhzdWJzZXQ9J0RUX0RBVEUnLCBrZWVwPSdsYXN0JykKCiMgRXh0cmFpbmRvIG8gcHJpbWVpcm8sIG8gw7psdGltbyBlIG8gcGVudWx0aW1vIHJlZ2lzdHJvcwpkZjIgPSBkZjEuaGVhZCgxKS5hcHBlbmQoZGYxLnRhaWwoMikpCgojIEFwcmVzZW50YW5kbyBvIHJlc3VsdGFkbwpwcmludChkZjIp"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
##
# By Sidon
# Resposta no STOpt  
# https://pt.stackoverflow.com/a/367662/7412
##

import pandas as pd

dados = [['22828949', '2019-02-26', '453.00'],
 ['22828949', '2019-02-22', '453.00'],
 ['22828949', '2019-02-18', '453.00'],
 ['22828949', '2019-02-05', '453.00'],
 ['22828950', '2019-02-26', '189.00'],
 ['22828950', '2019-02-24', '189.00']]

# Construindo o dataframe
df0 = pd.DataFrame(dados, columns=['ID', 'DT_DATE', 'NR_PRICE']).sort_values(by=['DT_DATE'])

# Removendo as datas duplicatas
df1 = df0.drop_duplicates(subset='DT_DATE', keep='last')

# Extraindo o primeiro, o último e o penultimo registros
df2 = df1.head(1).append(df1.tail(2))

# Apresentando o resultado
print(df2)