[英]Convert string formatted as Pandas DataFrame into an actual DataFrame
I am trying to convert a formatted string into a pandas data frame.我正在尝试将格式化的字符串转换为熊猫数据框。
[['CD_012','JM_022','PT_011','CD_012','JM_022','ST_049','MB_021','MB_021','CB_003'
,'FG_031','PC_004'],['NL_003','AM_006','MB_021'],
['JA_012','MB_021','MB_021','MB_021'],['JU_006'],
['FG_002','FG_002','CK_055','ST_049','NM_004','CD_012','OP_002','FG_002','FG_031',
'TG_005','SP_014'],['FG_002','FG_031'],['MD_010'],
['JA_012','MB_021','NL_003','MZ_020','MB_021'],['MB_021'],['PC_004'],
['MB_021','MB_021'],['AM_006','NM_004','TB_006','MB_021']]
I am trying to use the pandas.DataFrame
method to do so but the result is that this whole string is placed inside one element in the DataFrame
.我正在尝试使用pandas.DataFrame
方法来执行此操作,但结果是将整个字符串放置在DataFrame
一个元素内。
Is this what you mean?你是这个意思吗?
import pandas as pd
list_of_lists = [['CD_012','JM_022','PT_011','CD_012','JM_022','ST_049','MB_021','MB_021','CB_003'
,'FG_031','PC_004'],['NL_003','AM_006','MB_021'],
['JA_012','MB_021','MB_021','MB_021'],['JU_006'],
['FG_002','FG_002','CK_055','ST_049','NM_004','CD_012','OP_002','FG_002','FG_031',
'TG_005','SP_014'],['FG_002','FG_031'],['MD_010'],
['JA_012','MB_021','NL_003','MZ_020','MB_021'],['MB_021'],['PC_004'],
['MB_021','MB_021'],['AM_006','NM_004','TB_006','MB_021']]
result = pd.DataFrame({'result': list_of_lists})
Best approach would be to split the string with the '],[' delimeter and then convert to df.最好的方法是用 '],[' 分隔符分割字符串,然后转换为 df。
import numpy as np
import pandas as pd
def stringToDF(s):
array = s.split('],[')
# Adjust the constructor parameters based on your string
df = pd.DataFrame(data=array,
#index=array[1:,0],
#columns=array[0,1:]
)
print(df)
return df
stringToDF(s)
Good luck!祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.