简体   繁体   English

制作一个数据框,在每第 n 列之后创建新行,仅使用分号作为分隔符

[英]Making a dataframe where new row is created after every nth column using only semi colons as delimiters

I have the following string in a column within a row in a pandas dataframe.我在 pandas 数据框的一行中的一列中有以下字符串。 You could just treat it as a string.你可以把它当作一个字符串。

;2;613;12;1;Ajc hw EEE;13;.387639;1;EXP;13;2;128;12;1;NNN XX Ajc;13;.208966;1;SGX;13;..

It goes on like that.就这样继续下去。

I want to convert it into a table and use the semi colon ;我想将它转换成表格并使用分号; symbol as a delimiter.符号作为分隔符。 The problem is there is no new line delimiter and I have to estimate it to be every 10 items.问题是没有换行符,我必须估计它是每 10 个项目。

So, it should look something like this.所以,它应该看起来像这样。

;2;613;12;1;Ajc hw EEE;13;.387639;1;EXP;13;
 2;128;12;1;NNN XX Ajc;13;.208966;1;SGX;13;..

How do I convert that string into a new dataframe in pandas.如何将该字符串转换为 pandas 中的新数据框。 After every 10 semi colon delimiters, a new row should be created.在每 10 个分号分隔符之后,应创建一个新行。

I have no idea how to do this, any help would be greatly appreciated in terms of tools or ideas.我不知道如何做到这一点,在工具或想法方面的任何帮助将不胜感激。

This should work这应该工作

# removing first value as it's a semi colon
data = ';2;613;12;1;Ajc hw EEE;13;.387639;1;EXP;13;2;128;12;1;NNN XX Ajc;13;.208966;1;SGX;13;'[1:] 
data = data.split(';')
row_count = len(data)//10

data = [data[x*10:(x+1)*10] for x in range(row_count)]
pd.DataFrame(data)

I used a double slash for dividing but as your data length should be dividable by 10, you can use only one.我使用了双斜线来分隔,但由于您的数据长度应该可以被 10 整除,所以您只能使用一个。

Here's a screenshot of my output.这是我的输出的屏幕截图。 在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM