1

I have these 3 variables (list of list, except for distance):

unique_pitch_result = 
[[0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64], # len is dynamic
[0, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58], 
[0, 43, 44, 45, 46, 47, 48, 49, 50]]

pitch_class_result = 
[['0', 'F3', 'F#3 / Gb3', 'G3', 'G#3 / Ab3', 'A3', 'A#3 / Bb3', 'B3', 'C4', 'C#4 / Db4', 'D4', 'D#4 / Eb4', 'E4'], 
['0', 'C3', 'C#3 / Db3', 'D3', 'E3', 'F3', 'F#3 / Gb3', 'G3', 'G#3 / Ab3', 'A3', 'A#3 / Bb3'], 
['0', 'G2', 'G#2 / Ab2', 'A2', 'A#2 / Bb2', 'B2', 'C3', 'C#3 / Db3', 'D3']]

distance_result =
[2020.0, 611.0, 1782.0]

song_number = 3

I want to insert them per row into a DataFrame using this code:

import pandas as pd

def make_dataframe(unique_pitch_result, pitch_class_result, distance_result, song_number):
  print(song_number)
  
  df = pd.DataFrame(columns=('unique-pitch', 'pitch-class', 'distance'))

  for i in range(song_number):
    df.loc[i] = pd.Series({'unique-pitch': unique_pitch_result, 'pitch-class': pitch_class_result, 'distance': distance_result})
  
  return df

I am hoping to get this result:

  unique-pitch                                        pitch-class                                                                      distance              
0 [0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64] ['0', 'F3', 'F#3/Gb3', 'G3', 'G#3/Ab3', 'A3', 'A#3/Bb3', 'B3', 'C4', 'C#4/Db4', 'D4', 'D#4/Eb4', 'E4'] 2020.0
1 [0, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58]         ['0', 'C3', 'C#3/Db3', 'D3', 'E3', 'F3', 'F#3/Gb3', 'G3', 'G#3/Ab3', 'A3', 'A#3/Bb3']                  611.0
2 [0, 43, 44, 45, 46, 47, 48, 49, 50]                 ['0', 'G2', 'G#2/Ab2', 'A2', 'A#2/Bb2', 'B2', 'C3', 'C#3/Db3', 'D3']                                   1782.0

But instead I got this:

                                         unique-pitch  ...                 distance
0   [[0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 6...  ...  [2020.0, 611.0, 1782.0]
1   [[0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 6...  ...  [2020.0, 611.0, 1782.0]
2   [[0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 6...  ...  [2020.0, 611.0, 1782.0]

How should I fix the function make_dataframe?

1 Answer 1

2

You need indexing unique_pitch_result and pitch_class_result by i for get sublist by variable i:

def make_dataframe(unique_pitch_result, pitch_class_result, distance_result, song_number):
  print(song_number)
  
  df = pd.DataFrame(columns=('unique-pitch', 'pitch-class', 'distance'))

  for i in range(song_number):
    df.loc[i] = pd.Series({'unique-pitch': unique_pitch_result[i], 'pitch-class': pitch_class_result[i], 'distance': distance_result[i]})
  
  return df

print (make_dataframe(unique_pitch_result, pitch_class_result, distance_result, song_number))

Non loop alternative with numpy.tile and pass all lists:

def make_dataframe(unique_pitch_result, pitch_class_result, distance_result, song_number):
    print(song_number)

    return pd.DataFrame({'unique-pitch': unique_pitch_result,
                        'pitch-class': pitch_class_result,
                        'distance': distance_result})
  


print (make_dataframe(unique_pitch_result, pitch_class_result, distance_result, song_number))
                                        unique-pitch  \
0  [0, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63...   
1        [0, 48, 49, 50, 52, 53, 54, 55, 56, 57, 58]   
2                [0, 43, 44, 45, 46, 47, 48, 49, 50]   

                                         pitch-class  distance  
0  [0, F3, F#3 / Gb3, G3, G#3 / Ab3, A3, A#3 / Bb...   2020.0 
1  [0, C3, C#3 / Db3, D3, E3, F3, F#3 / Gb3, G3, ...    611.0  
2  [0, G2, G#2 / Ab2, A2, A#2 / Bb2, B2, C3, C#3 ...   1782.0 
Sign up to request clarification or add additional context in comments.

1 Comment

the distance should be separated too, but now I know how to do it. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.