0
$\begingroup$

How do you convert something like this:

A: 1
B: 2
C: 3
###
A: 5
B: 5
C: 6
###
A: 2
B: 5
C: 7

into a dataset where the first row would be the first section with

A as column-1 B as column-2 and C as column-3

so we get this:

 A B C
 1 2 3
 5 5 6
 2 5 7
$\endgroup$

closed as off-topic by Toros91, Pedro Henrique Monforte, oW_ Apr 22 at 3:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about data science, within the scope defined in the help center." – Toros91, Pedro Henrique Monforte, oW_
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ How exactly is the initial data stored? List of dictionaries? $\endgroup$ – Ben Reiniger Apr 21 at 17:02
  • 1
    $\begingroup$ It is stored as a plain text file $\endgroup$ – OcK Apr 21 at 17:15
2
$\begingroup$

If I understand this correctly your sequence is always 3 elements. Then you can do this:

a = ['A:1','B:2','C:3','A:5','B:5','C:6','A:2','B:5','C:7']
b = []
rep_len = 3

# Looping with step size equal to repetition length
for i in range(0,len(a),rep_len):

    # Selecting a repetition length
    c = a[i:i+rep_len]

    # Extracting everything in after letter and colon and casting to integer
    c = [int(x[2:]) for x in c]

    # Append to a list of lists
    b.append(c)

df = pd.DataFrame(b, columns=['A', 'B', 'C'])

Resulting in:

    A   B   C
0   1   2   3
1   5   5   6
2   2   5   7
$\endgroup$
  • 1
    $\begingroup$ Exactly what I asked for - Thanks! $\endgroup$ – OcK Apr 21 at 18:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.