DataFrame of frequencies by list comprehension?










1














I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). Essentially, I want it to look like this...



df = pd.DataFrame(columns=['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])
df.loc[0] = (55, 58.27, 61.74, 32.7, 34.65, 36.71, 38.89, 41.2, 43.65, 49, 51.91)


But without having to manually assign all the frequencies to their respective notes and with an octave per row (octave 1 to 8).



Based on the site http://pages.mtu.edu/~suits/notefreqs.html, the space between each note (or a 'half-step') given a single note is...



def hz_stepper(fixed_note, steps):
a = 2 ** (1/12)
return fixed_note * a ** steps


Using that function 'hz_stepper', I can chromatically increase or decrease a given note n times by assigning 1 or -1 to steps variable.



My question is, how do I create a DataFrame where all the rows look like how I did it manually, but using a list comprehension to form the rows?










share|improve this question























  • BTW your octave above starts with C instead of A
    – Matthias Ossadnik
    Nov 9 at 21:23















1














I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). Essentially, I want it to look like this...



df = pd.DataFrame(columns=['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])
df.loc[0] = (55, 58.27, 61.74, 32.7, 34.65, 36.71, 38.89, 41.2, 43.65, 49, 51.91)


But without having to manually assign all the frequencies to their respective notes and with an octave per row (octave 1 to 8).



Based on the site http://pages.mtu.edu/~suits/notefreqs.html, the space between each note (or a 'half-step') given a single note is...



def hz_stepper(fixed_note, steps):
a = 2 ** (1/12)
return fixed_note * a ** steps


Using that function 'hz_stepper', I can chromatically increase or decrease a given note n times by assigning 1 or -1 to steps variable.



My question is, how do I create a DataFrame where all the rows look like how I did it manually, but using a list comprehension to form the rows?










share|improve this question























  • BTW your octave above starts with C instead of A
    – Matthias Ossadnik
    Nov 9 at 21:23













1












1








1







I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). Essentially, I want it to look like this...



df = pd.DataFrame(columns=['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])
df.loc[0] = (55, 58.27, 61.74, 32.7, 34.65, 36.71, 38.89, 41.2, 43.65, 49, 51.91)


But without having to manually assign all the frequencies to their respective notes and with an octave per row (octave 1 to 8).



Based on the site http://pages.mtu.edu/~suits/notefreqs.html, the space between each note (or a 'half-step') given a single note is...



def hz_stepper(fixed_note, steps):
a = 2 ** (1/12)
return fixed_note * a ** steps


Using that function 'hz_stepper', I can chromatically increase or decrease a given note n times by assigning 1 or -1 to steps variable.



My question is, how do I create a DataFrame where all the rows look like how I did it manually, but using a list comprehension to form the rows?










share|improve this question















I'm trying to build a pandas DataFrame of chromatic frequencies between A1 (55Hz) and A8 (7040Hz). Essentially, I want it to look like this...



df = pd.DataFrame(columns=['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#'])
df.loc[0] = (55, 58.27, 61.74, 32.7, 34.65, 36.71, 38.89, 41.2, 43.65, 49, 51.91)


But without having to manually assign all the frequencies to their respective notes and with an octave per row (octave 1 to 8).



Based on the site http://pages.mtu.edu/~suits/notefreqs.html, the space between each note (or a 'half-step') given a single note is...



def hz_stepper(fixed_note, steps):
a = 2 ** (1/12)
return fixed_note * a ** steps


Using that function 'hz_stepper', I can chromatically increase or decrease a given note n times by assigning 1 or -1 to steps variable.



My question is, how do I create a DataFrame where all the rows look like how I did it manually, but using a list comprehension to form the rows?







python pandas numpy list-comprehension music






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 21:09

























asked Nov 9 at 20:56









sam-la

63




63











  • BTW your octave above starts with C instead of A
    – Matthias Ossadnik
    Nov 9 at 21:23
















  • BTW your octave above starts with C instead of A
    – Matthias Ossadnik
    Nov 9 at 21:23















BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23




BTW your octave above starts with C instead of A
– Matthias Ossadnik
Nov 9 at 21:23












1 Answer
1






active

oldest

votes


















1














just iterate over the pitches and reshape the result afterwards:



import numpy as np
import pandas as pd

base = 55.
n_octave = 8
columns = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']

factors = 2**(np.arange(12 * n_octave) / 12.)
pd.DataFrame(data=base * factors.reshape((n_octave, 12)), columns=columns)


Explanation



factors are the desired frequencies as 1d numpy array, but they are not in the tabular form required for the DataFrame. reshape creates a view of the array content, that has shape (n_octave, 12) such that rows are contiguous. E.g.



>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])


This is just the format needed for the DataFrame.






share|improve this answer






















  • Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
    – sam-la
    Nov 9 at 21:44










  • @sam-la added explanation
    – Matthias Ossadnik
    Nov 9 at 22:16










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233168%2fdataframe-of-frequencies-by-list-comprehension%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














just iterate over the pitches and reshape the result afterwards:



import numpy as np
import pandas as pd

base = 55.
n_octave = 8
columns = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']

factors = 2**(np.arange(12 * n_octave) / 12.)
pd.DataFrame(data=base * factors.reshape((n_octave, 12)), columns=columns)


Explanation



factors are the desired frequencies as 1d numpy array, but they are not in the tabular form required for the DataFrame. reshape creates a view of the array content, that has shape (n_octave, 12) such that rows are contiguous. E.g.



>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])


This is just the format needed for the DataFrame.






share|improve this answer






















  • Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
    – sam-la
    Nov 9 at 21:44










  • @sam-la added explanation
    – Matthias Ossadnik
    Nov 9 at 22:16















1














just iterate over the pitches and reshape the result afterwards:



import numpy as np
import pandas as pd

base = 55.
n_octave = 8
columns = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']

factors = 2**(np.arange(12 * n_octave) / 12.)
pd.DataFrame(data=base * factors.reshape((n_octave, 12)), columns=columns)


Explanation



factors are the desired frequencies as 1d numpy array, but they are not in the tabular form required for the DataFrame. reshape creates a view of the array content, that has shape (n_octave, 12) such that rows are contiguous. E.g.



>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])


This is just the format needed for the DataFrame.






share|improve this answer






















  • Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
    – sam-la
    Nov 9 at 21:44










  • @sam-la added explanation
    – Matthias Ossadnik
    Nov 9 at 22:16













1












1








1






just iterate over the pitches and reshape the result afterwards:



import numpy as np
import pandas as pd

base = 55.
n_octave = 8
columns = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']

factors = 2**(np.arange(12 * n_octave) / 12.)
pd.DataFrame(data=base * factors.reshape((n_octave, 12)), columns=columns)


Explanation



factors are the desired frequencies as 1d numpy array, but they are not in the tabular form required for the DataFrame. reshape creates a view of the array content, that has shape (n_octave, 12) such that rows are contiguous. E.g.



>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])


This is just the format needed for the DataFrame.






share|improve this answer














just iterate over the pitches and reshape the result afterwards:



import numpy as np
import pandas as pd

base = 55.
n_octave = 8
columns = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']

factors = 2**(np.arange(12 * n_octave) / 12.)
pd.DataFrame(data=base * factors.reshape((n_octave, 12)), columns=columns)


Explanation



factors are the desired frequencies as 1d numpy array, but they are not in the tabular form required for the DataFrame. reshape creates a view of the array content, that has shape (n_octave, 12) such that rows are contiguous. E.g.



>>> np.arange(6).reshape((2, 3))
array([[0, 1, 2],
[3, 4, 5]])


This is just the format needed for the DataFrame.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 22:16

























answered Nov 9 at 21:22









Matthias Ossadnik

57427




57427











  • Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
    – sam-la
    Nov 9 at 21:44










  • @sam-la added explanation
    – Matthias Ossadnik
    Nov 9 at 22:16
















  • Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
    – sam-la
    Nov 9 at 21:44










  • @sam-la added explanation
    – Matthias Ossadnik
    Nov 9 at 22:16















Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44




Thank you! Could you possibly explain what exactly reshape is doing here? I'm reading the documentation but am having a hard time understanding.
– sam-la
Nov 9 at 21:44












@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16




@sam-la added explanation
– Matthias Ossadnik
Nov 9 at 22:16

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233168%2fdataframe-of-frequencies-by-list-comprehension%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)