Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get value from numpy array into pandas dataframe

I’m trimming some audio files with librosa. This works fine, but instead of saving the trimmed file, I would like to get the length before trimming and after trimming. The code I have now can print all the information I need, I just need to get it into a pandas data frame where column1 = filename, column2 = length before trim, column3 = length after trim. I don’t need ‘sr’. I can’t seem to figure out how to do that right… help appreciated.

here’s the code:

for file in files:
    print(file)
    audio, sr = librosa.load(file, sr= 16000, mono=True)
    print(audio.shape, sr)
    trimmed = librosa.effects.trim(audio, top_db= 45)
    print(trimmed[0].shape, sr)
    

here’s the output I’m getting with the print command:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

/Desktop/py_scripts/audio_experiments/wav/1031_8637_73170.wav
(39680,) 16000
(38144,) 16000
/Desktop/py_scripts/audio_experiments/wav/1060_8777_76783.wav
(28160,) 16000
(28160,) 16000
/Desktop/py_scripts/audio_experiments/wav/1128_8634_82873.wav
(74240,) 16000
(74240,) 16000

>Solution :

You should collect the data into lists or a dict and then use it to construct a dataframe

import pandas as pd

file_lst, len_before_lst, len_after_lst = [], [], []
for file in files:
    file_lst.append(file)
    audio, sr = librosa.load(file, sr= 16000, mono=True)
    len_before_lst.append(audio.shape[0])
    trimmed = librosa.effects.trim(audio, top_db= 45)
    len_after_lst.append(trimmed[0].shape)

df = pd.DataFrame([file_lst, len_before_lst, len_after_lst], index=['filename', 'len_before', 'len_after']).T
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading