【问题标题】:convert text to dataframe in python在python中将文本转换为数据框
【发布时间】:2020-07-11 15:31:01
【问题描述】:

我收到了我的 API 响应并将其转换为文本。结果如下:

'timestamp,open,high,low,close,volume\r\n2020-07-10 19:50:00,0.6020,0.6020,0.6020,0.6020,2436\r\n2020-07-10 19:40:00,0.6005,0.6005,0.6005,0.6005,1000\r\n2020-07-10 19:15:00,0.6200,0.6220,0.6200,0.6220,2200\r\n2020-07-10 19:05:00,0.6100,0.6100,0.6100,0.6100,1000\r\n2020-07-10
...' 

如何将其转换为 csv?是否可以在不必将 csv 文件保存在我的设备上的情况下执行此操作?

【问题讨论】:

标签: python dataframe


【解决方案1】:

您的数据已经是 CSV。只需使用StringIO 执行以下操作:

import pandas as pd
from io import StringIO

data = 'timestamp,open,high,low,close,volume\r\n2020-07-10 19:50:00,0.6020,0.6020,0.6020,0.6020,2436\r\n2020-07-10 19:40:00,0.6005,0.6005,0.6005,0.6005,1000\r\n2020-07-10 19:15:00,0.6200,0.6220,0.6200,0.6220,2200\r\n2020-07-10 19:05:00,0.6100,0.6100,0.6100,0.6100,1000\r\n2020-07-10'
pd.read_csv(StringIO(data))

输出是:

             timestamp    open    high     low   close  volume
0  2020-07-10 19:50:00  0.6020  0.6020  0.6020  0.6020  2436.0
1  2020-07-10 19:40:00  0.6005  0.6005  0.6005  0.6005  1000.0
2  2020-07-10 19:15:00  0.6200  0.6220  0.6200  0.6220  2200.0
3  2020-07-10 19:05:00  0.6100  0.6100  0.6100  0.6100  1000.0
4           2020-07-10     NaN     NaN     NaN     NaN     NaN

【讨论】:

  • 这正是我寻找的!非常感谢!
【解决方案2】:

如果此答案有帮助,请在此处对已接受的答案进行投票,我从中获取并稍作修改:How to convert string that uses "\r\n" as line breaks to pandas dataframe

import pandas as pd
s = b'timestamp,open,high,low,close,volume\r\n2020-07-10 19:50:00,0.6020,0.6020,0.6020,0.6020,2436\r\n2020-07-10 19:40:00,0.6005,0.6005,0.6005,0.6005,1000\r\n2020-07-10 19:15:00,0.6200,0.6220,0.6200,0.6220,2200\r\n2020-07-10 19:05:00,0.6100,0.6100,0.6100,0.6100,1000\r\n2020-07-10' 
data = list(map(lambda x: x.split(','),s.decode('utf-8').split("\r\n")))
df = pd.DataFrame(data[1:], columns=data[0])
df

输出:

    timestamp           open    high    low    close    volume
0   2020-07-10 19:50:00 0.6020  0.6020  0.6020  0.6020  2436
1   2020-07-10 19:40:00 0.6005  0.6005  0.6005  0.6005  1000
2   2020-07-10 19:15:00 0.6200  0.6220  0.6200  0.6220  2200
3   2020-07-10 19:05:00 0.6100  0.6100  0.6100  0.6100  1000
4   2020-07-10          None    None    None    None    None

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 2023-03-14
    相关资源
    最近更新 更多