如何在python中将文本字符串列表转换为熊猫数据框?
Posted
技术标签:
【中文标题】如何在python中将文本字符串列表转换为熊猫数据框?【英文标题】:How to convert a list of text string to a pandas dataframe in python? 【发布时间】:2021-03-22 18:35:38 【问题描述】:我有一个代表行和列的文本字符串列表,我想将其转换为 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']
【问题讨论】:
【参考方案1】:试试这段代码:
locatio_to_csv = '../sample.csv'
input_df = pd.read_csv(locatio_to_csv,
names=['column_name1', 'column_name2', 'column_name3'..])
【讨论】:
【参考方案2】:试试:
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中将文本字符串列表转换为熊猫数据框?的主要内容,如果未能解决你的问题,请参考以下文章