简体   繁体   中英

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:

/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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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