csv 파일을 읽고 첫 번째 열을 DatetimeIndex로 설정하여 timeseries 'aapl'을 만들고 싶습니다.
다음은 csv 파일의 몇 줄입니다.
2000-01-03, 111.937502
2000-01-04, 102.500003
2000-01-05, 103.999997
2000-01-06, 94.999998
2000-01-07, 99.500001
결과 쇼는 다음과 같습니다.
In [1]: aapl.head()
Out[1]:
Date
2000-01-03 111.937502
2000-01-04 102.500003
2000-01-05 103.999997
2000-01-06 94.999998
2000-01-07 99.500001
Name: AAPL, dtype: float64
In [2]: type(aapl)
Out[2]: pandas.core.series.Series
In [3]: type(aapl.index)
Out[3]: pandas.tseries.index.DatetimeIndex
나는 시도했다 :
aapl = pd.read_csv('aapl.csv', header=None)
aapl[0] = pd.to_datetime(aapl[0])
aapl.set_index(0, inplace=True)
aapl.index.name = 'Date'
print(type(aapl))
print(type(aapl.index))
print(aapl.head())
그러나 그것은 나를 남깁니다.
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
1
Date
2000-01-03 111.937502
2000-01-04 102.500003
2000-01-05 103.999997
2000-01-06 94.999998
2000-01-07 99.500001
시리즈가 아니라 여전히 데이터 프레임입니다. 그리고 값이있는 열에는 여전히 열 이름이 있습니다.
모든 제안을 환영합니다!
주로 squeeze
변환 매개 변수 를 사용할 수 있다고 생각합니다 Series
.
import pandas as pd
from pandas.compat import StringIO
temp=u"""2000-01-03,111.937502
2000-01-04,102.500003
2000-01-05,103.999997
2000-01-06,94.999998
2000-01-07,99.500001"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
aapl = pd.read_csv(StringIO(temp),
squeeze=True,
index_col=[0],
parse_dates=True,
names=['Date','col'])
print(type(aapl))
<class 'pandas.core.series.Series'>
print(type(aapl.index))
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>
print(aapl.head())
Date
2000-01-03 111.937502
2000-01-04 102.500003
2000-01-05 103.999997
2000-01-06 94.999998
2000-01-07 99.500001
Name: col, dtype: float64
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다