gpt4 book ai didi

python-3.x - 如何在python中将文本字符串列表转换为pandas数据框?

转载 作者:行者123 更新时间:2023-12-04 08:26:11 24 4
gpt4 key购买 nike

我有一个表示行和列的文本字符串列表,我想将其转换为 Pandas 数据框。
现在我使用文本复制粘贴到编辑器,然后将其保存为 csv 文件,然后使用 pd.read_csv() 读取它.我确信这可以以某种方式自动化。
对于可重现的示例,请考虑以下列表:

  ['Symbol,Description,Qty,Trade Price,Mark,Mark Value,P/L Day,P/L Open,P/L ', 'AXTI,AXT INC COM,+90,10.79,9.46,851.40,0.00,-119.70,-12.33', 'INTT,INTEST CORP COM,+50,5.64,5.02,251.00,0.00,-31.00,-10.99', 'ACMR,ACM RESEARCH INC COM CL A,+201,80.9374,73.20,14713.20,0.00,-1,555.21,-9.56', 'ASUR,ASURE SOFTWARE INC COM,+90,7.70,7.20,648.00,0.00,-45.00,-6.49']

最佳答案

尝试:

import io
import pandas as pd
df = pd.read_csv(io.StringIO("""
Symbol,Description,Qty,Trade Price,Mark,Mark Value,P/L Day,P/L Open,P/L
AXTI,AXT INC COM,+90,10.79,9.46,851.40,0.00,(119.70),-12.33
INTT,INTEST CORP COM,+50,5.64,5.02,251.00,0.00,(31.00),-10.99
ACMR,ACM RESEARCH INC COM CL A,+201,80.9374,73.20,14713.20,0.00,(1555.21),-9.56
ASUR,ASURE SOFTWARE INC COM,+90,7.70,7.20,648.00,0.00,(45.00),-6.49
"""), sep=r",", engine="python")
打印:
 Symbol                Description  Qty  Trade Price   Mark  Mark Value  \
0 AXTI AXT INC COM 90 10.7900 9.46 851.4
1 INTT INTEST CORP COM 50 5.6400 5.02 251.0
2 ACMR ACM RESEARCH INC COM CL A 201 80.9374 73.20 14713.2
3 ASUR ASURE SOFTWARE INC COM 90 7.7000 7.20 648.0

P/L Day P/L Open P/L
0 0.0 (119.70) -12.33
1 0.0 (31.00) -10.99
2 0.0 (1555.21) -9.56
3 0.0 (45.00) -6.49
注意:我们正在使用 comma解析字符串的分隔符。 sep=r"," .因此,对于您的示例,我不得不从 (1,555.21) 中删除逗号。

关于python-3.x - 如何在python中将文本字符串列表转换为pandas数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65249751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com