[英]Converting string with comma delimited data and newline character to pandas dataframe
I'm pulling 1 minute historical bars for a stock and the data comes in like this: 我正在为某股票拉入1分钟的历史柱线,数据如下所示:
'2018-06-11 09:31:00,968.250,965.000,968.000,965.250,17220,1160\n2018-06-11
09:32:00,965.250,964.250,965.250,964.750,17872,611\n2018-06-11
09:33:00,965.000,963.250,965.000,963.500,18851,547\n'
It's one string where each row is separated by the new line character and each field is separated by a comma. 这是一个字符串,其中的每一行都由换行符分隔,而每个字段均由逗号分隔。 It looks fine when I use the print() function but I want to convert this into a pandas dataframe.
使用print()函数时看起来不错,但我想将其转换为pandas数据框。 I appreciate any help.
感谢您的帮助。
This works fine when feeding the string to pandas.read_csv
: 将字符串输入
pandas.read_csv
时,此方法工作正常:
import pandas as pd
from io import StringIO
mystr = StringIO("""2018-06-11 09:31:00,968.250,965.000,968.000,965.250,17220,1160\n2018-06-11 09:32:00,965.250,964.250,965.250,964.750,17872,611\n2018-06-11 09:33:00,965.000,963.250,965.000,963.500,18851,547\n""")
df = pd.read_csv(mystr, index_col=0, header=None)
df.index = pd.to_datetime(df.index)
print(df)
1 2 3 4 5 6
0
2018-06-11 09:31:00 968.25 965.00 968.00 965.25 17220 1160
2018-06-11 09:32:00 965.25 964.25 965.25 964.75 17872 611
2018-06-11 09:33:00 965.00 963.25 965.00 963.50 18851 547
print(df.dtypes)
1 float64
2 float64
3 float64
4 float64
5 int64
6 int64
dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.