简体   繁体   English

如何将此列表 object 转换为 dataframe?

[英]How can i convert this lists object into a dataframe?

I have something which lokos like (called lines)我有一些 lokos 喜欢的东西(称为线条)

['                id\t    Name\t    Type\t      User\t     Q\t             country\t       Final-score\t       Progress\t                       website',
 'abcde\t                 jen\t      engineer\t  jenabc\t           RUNNING\t         UK\t             75%\t                                N/A',
 'fres\t        Penny\t               dr\t     dr123\t           RUNNING\t         DENMARK\t             67%\t                                N/A'] 

each line which is in the speech marks and separated by ',' is a dataframe row.语音标记中以“,”分隔的每一行是 dataframe 行。 However i cannot convert to dataframe.但是我无法转换为 dataframe。

new_df = pd.read_csv(StringIO(",".join(lines[1:])),sep = "\t") 

i do [1:] since the first line is just a comment.我做 [1:] 因为第一行只是一个评论。 i get the error: ParserError: Error tokenizing data. C error: Expected 963 fields in line 3, saw 1099我收到错误: ParserError: Error tokenizing data. C error: Expected 963 fields in line 3, saw 1099 ParserError: Error tokenizing data. C error: Expected 963 fields in line 3, saw 1099

i'd like my datframe to be such that the first row is the headers, and the rest are the contents separated by \t.我希望我的 datframe 的第一行是标题,而 rest 是由 \t 分隔的内容。 how can i do this?我怎样才能做到这一点?

df = pd.read_csv(StringIO("\n".join(lines)), sep=r"\s+")
print(df)

Prints:印刷:

      id   Name      Type    User        Q  country Final-score  Progress  website
0  abcde    jen  engineer  jenabc  RUNNING       UK         75%       NaN      NaN
1   fres  Penny        dr   dr123  RUNNING  DENMARK         67%       NaN      NaN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Pandas DataFrame 的列转换为列表列表? - How can I convert columns of a pandas DataFrame into a list of lists? 如何将 dataframe 列从 object 转换为浮动 - How can I convert a column of dataframe from object to float 如何将 Sqlalchemy 表 object 转换为 Pandas DataFrame? - How can I convert Sqlalchemy table object to Pandas DataFrame? 如何在 Pyspark 的 Dataframe 中转换列表列表,每个列表都是每个属性的值? - How can I convert a list of lists in a Dataframe in Pyspark, being each list the values of each attribute? 如何将字典列表和嵌套列表转换为 pandas Dataframe 以用于 markdown 表生成器? - How can I convert a list of dictionaries and nested lists into a pandas Dataframe for use in a markdown table generator? 如何在Python中将数据框中的行转换为多个列表 - how do I convert rows in a dataframe into multiple lists in Python 如何从列表列表转换为Pandas DataFrame - How do I to convert from a list of list of lists to pandas DataFrame 如何在Python中将列表列表转换为列表字典? - How can I convert a list of lists into a dictionary of lists in Python? 如何将 groupby object 转换为列表列表和 append 将新列/值转换为列表中的列表 - How can I convert a groupby object to a list of lists and append a new column/value to the list's within the list 如何获取包含字符串列表的 dataframe 并从 Pyspark 中的这些列表中创建另一个 dataframe? - How can I take a dataframe containing lists of strings and create another dataframe from these lists in Pyspark?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM