因工作原因,一些获取的行业数据以已知的结构体存储在.mat文件中,
现需要将其存储在数据库中并且能够灵活调用至python dataframe里进行操作
原数据的一个例子如下
目标如上:
然后是转化代码:
import scipy.io
data = scipy.io.loadmat(r'C:\Users\wenzhe.tian\Desktop\PTSimA\Doing\MC.mat')
import pandas as pd
data.pop('__header__')
data.pop('__version__')
data.pop('__globals__')
vehicle_name=data.keys()
vehicle_name=list(vehicle_name)
for i in vehicle_name:
df = pd.DataFrame(data[i][0])
try:
df=df.astype(float)
except:
for j in list(df):
try:
df[j]=df[j].astype(float)
except:
continue
# df[j]=df[j].astype(str)
if i==vehicle_name[0]:
df1=df;
else:
df1=pd.concat([df,df1],axis=0)
df1['MC_name']=vehicle_name
df1['Tips']=df1['Tips'].map(str)+df1['tips'].map(str)
df1['Tips']=df1['Tips'].str.replace('nan','')
df1=df1.drop(['tips'],axis=1)
df1=df1.reset_index();
import numpy as np
# ndarray需转化为 字符
list_transfer=['Speed','Torque','eff','eff_current']
for i in list_transfer:
for j in range(len(df1)):
try:
df1[i][j]=df1[i][j].tostring();
except:
continue;
结果如下(df1):
然后用to_sql的方式将该dataframe 保存至本地sql数据库即可