Difference between revisions of "Panda: read csv datetime"

From OnnoWiki
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
==Cara 1==
 +
 +
headers = ['col1', 'col2', 'col3', 'col4']
 +
dtypes = {'col1': 'str', 'col2': 'str', 'col3': 'str', 'col4': 'float'}
 +
parse_dates = ['col1', 'col2']
 +
pd.read_csv(file, sep='\t', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)
 +
 +
 +
headers = ['Time', 'Value']
 +
dtypes = {'Time': 'str', 'Value': 'float'}
 +
parse_dates = ['Time']
 +
pd.read_csv(file, sep='\t', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)
 +
 +
 
==Cara 1==
 
==Cara 1==
  
Line 7: Line 21:
 
  print(x)
 
  print(x)
 
  x.info()
 
  x.info()
 +
print(x.head())
 +
  
 
==Cara 2==
 
==Cara 2==
 +
 +
df = pd.read_csv(file, sep = ',', parse_dates= [col],encoding='utf-8-sig', usecols= ['Date', 'ids'],)
 +
 +
 +
==Cara 3==
  
 
  import numpy as np
 
  import numpy as np

Latest revision as of 10:10, 8 August 2019

Cara 1

headers = ['col1', 'col2', 'col3', 'col4']
dtypes = {'col1': 'str', 'col2': 'str', 'col3': 'str', 'col4': 'float'}
parse_dates = ['col1', 'col2']
pd.read_csv(file, sep='\t', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)


headers = ['Time', 'Value']
dtypes = {'Time': 'str', 'Value': 'float'}
parse_dates = ['Time']
pd.read_csv(file, sep='\t', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)


Cara 1

import pandas
x = pandas.read_csv('/home/onno/TensorFlow/TEMP-train.csv', parse_dates=True, index_col='DateTime', 
                                names=['DateTime', 'X'], header=None, sep=',')
print(x)
x.info()
print(x.head())


Cara 2

df = pd.read_csv(file, sep = ',', parse_dates= [col],encoding='utf-8-sig', usecols= ['Date', 'ids'],)


Cara 3

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

infile = '/home/onno/TensorFlow/TEMP-train.csv'
mydateparser = lambda x: pd.datetime.strptime(x, "%Y-%m-%d %H:%M:%S")
df = pd.read_csv(infile, sep=',', names=['datetime', 'value'], parse_dates=['datetime'], date_parser=mydateparser)