用python做科技计算时,经常需要类型转换,以下是常用类型转换
一、ndarray 转换为 series
1、如果ndarray是二维数组,如下
array([[1],
[2],
[3]])
需要通过map结合lamdba
import numpy as np
import pandas as pd
data = np.array([1, 2, 3]).reshape(3, 1)
data_list = map(lambda x: x[0], data)
ser = pd.Series(data_list)
2、如果ndarray是一维数组,如下
array([1, 2, 3])
则通过data.tolist()即可
import numpy as np
import pandas as pd
data = np.array([1, 2, 3])
ser = pd.Series(data.tolist())
二、series转换为ndarray
通过Series.values实现series转换为ndarray
import pandas as pd
data = [['2019/08/01', 10],
['2019/08/01', 11]]
result = pd.DataFrame(data, columns=['ds', 'val'])
result['val'].values
data2 = pd.Series([1, 2