如何使用numpy ndarray列表创建pandas数据帧列?

2024-03-29 12:23:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个分别为2维和1维的numpy nd数组列表。我想使用作为我的数据框的2列的2个列表创建一个熊猫数据框。本人谨此陈辞:


Mfcc = np.arange(1,11,1)
Mfcc =  np.reshape(Mfcc, (-1, 2))
Mel_spec = np.arange(1,10,2)

df = pd.DataFrame(list(zip(Mfcc, Mel_spec)), 
               columns =['mfcc', 'mel_spectogram'])   ### mfcc is 2d and mel_spectogram is 1d
                   
type(df.mfcc[1])   ### returns string, when it should return a 1d array
type(df.mel_spectogram[1])   ### returns string, when it should return float being the first element of the array

我如何才能做到这一点?我可能在哪里犯错误


Tags: 数据df列表stringistypenpreturns
1条回答
网友
1楼 · 发布于 2024-03-29 12:23:43
import numpy as np
import pandas as pd

Mfcc = np.arange(1,11,1)
Mfcc =  np.reshape(Mfcc, (-1, 2))
Mel_spec = np.arange(1,10,2)

df = pd.DataFrame({'mfcc': list(Mfcc), 'mel_spectogram': Mel_spec})

df.mfcc[1], df.mel_spectogram[1]

此代码正在工作,请根据此代码修复Mfcc和Mel_规范

相关问题 更多 >