文章目录
- 创建ndarray类型数据
- 使用list创建series
- series和ndarray转化
- 使用list创建dataframe
- dataframe和ndarray之间的转化
- dataframe和series之间的转化
创建ndarray类型数据
import numpy as np
data = [["2017-10-18", 10.53, 10.69, 10.70, 10.51, 871365.0],
["2017-10-19", 10.64, 10.63, 10.72, 10.57, 722764.0],
["2017-10-20", 10.59, 10.48, 10.59, 10.41, 461808.0],
["2017-10-23", 10.39, 10.19, 10.40, 10.15, 1074465.0]]
arr_1 = np.array(data)
print(arr_1)
print(type(arr_1))
- 结果
![ndarray、series和dataframe类型转化_第2张图片](http://img.e-com-net.com/image/info8/a621535190de4d93ae6e9babf0be387f.jpg)
使用list创建series
- 使用pandas库构建series, index构造series的行索引
import numpy as np
import pandas as pd
data = [["2017-10-18", 10.53, 10.69, 10.70, 10.51, 871365.0],
["2017-10-19", 10.64, 10.63, 10.72, 10.57, 722764.0],
["2017-10-20", 10.59, 10.48, 10.59, 10.41, 461808.0],
["2017-10-23", 10.39, 10.19, 10.40, 10.15, 1074465.0]]
series = pd.Series(data, index=['a', 'b', 'c', 'd'])
print(series)
print(type(series))
- 结果
![ndarray、series和dataframe类型转化_第3张图片](http://img.e-com-net.com/image/info8/4d53dec12e974027b1e0d1fda1ae8167.jpg)
series和ndarray转化
- 使用变量名.values实现转化
- 使用变量名.as_matrix()会报错AttributeError: ‘Series’ object has no attribute ‘as_matrix’
arr = series.values
print(arr)
print(type(arr))
- 结果
![ndarray、series和dataframe类型转化_第4张图片](http://img.e-com-net.com/image/info8/cc879f8c76e9452ca3c0f3f23a8487b4.jpg)
使用list创建dataframe
- 使用pandas库构建dataframe,index和columns构造dataframe的行列索引
import pandas as pd
data = [["2017-10-18", 10.53, 10.69, 10.70, 10.51, 871365.0],
["2017-10-19", 10.64, 10.63, 10.72, 10.57, 722764.0],
["2017-10-20", 10.59, 10.48, 10.59, 10.41, 461808.0],
["2017-10-23", 10.39, 10.19, 10.40, 10.15, 1074465.0]]
df = pd.DataFrame(data, index=["2017-10-18", "2017-10-19", "2017-10-20", "2017-10-23"],
columns=['date',"open", "close", "high", "low", "volume"])
print(df)
print(type(df))
- 结果
![ndarray、series和dataframe类型转化_第5张图片](http://img.e-com-net.com/image/info8/c4c551bcd2864b8c9934f1bcb8d12da4.jpg)
dataframe和ndarray之间的转化
arr_1 = df.values
print(arr_1)
print(type(arr_1))
- 结果
![ndarray、series和dataframe类型转化_第6张图片](http://img.e-com-net.com/image/info8/17e0ddf691514f2086ae51eae08acde6.jpg)
dataframe和series之间的转化
series_open = df["open"]
print(series_open)
print(type(series_open))
-
结果
![ndarray、series和dataframe类型转化_第7张图片](http://img.e-com-net.com/image/info8/3a0c88df789a41c98c3c309e68ef50ff.jpg)
-
完整代码
import numpy as np
import pandas as pd
data = [["2017-10-18", 10.53, 10.69, 10.70, 10.51, 871365.0],
["2017-10-19", 10.64, 10.63, 10.72, 10.57, 722764.0],
["2017-10-20", 10.59, 10.48, 10.59, 10.41, 461808.0],
["2017-10-23", 10.39, 10.19, 10.40, 10.15, 1074465.0]]
series = pd.Series(data, index=['a', 'b', 'c', 'd'])
print(series)
print(type(series))
arr = series.values
print(arr)
print(type(arr))
import pandas as pd
data = [["2017-10-18", 10.53, 10.69, 10.70, 10.51, 871365.0],
["2017-10-19", 10.64, 10.63, 10.72, 10.57, 722764.0],
["2017-10-20", 10.59, 10.48, 10.59, 10.41, 461808.0],
["2017-10-23", 10.39, 10.19, 10.40, 10.15, 1074465.0]]
df = pd.DataFrame(data, index=["2017-10-18", "2017-10-19", "2017-10-20", "2017-10-23"],
columns=['date',"open", "close", "high", "low", "volume"])
print(df)
print(type(df))
arr_1 = df.values
print(arr_1)
print(type(arr_1))
series_open = df["open"]
print(series_open)
print(type(series_open))
import numpy as np
data = [["2017-10-18", 10.53, 10.69, 10.70, 10.51, 871365.0],
["2017-10-19", 10.64, 10.63, 10.72, 10.57, 722764.0],
["2017-10-20", 10.59, 10.48, 10.59, 10.41, 461808.0],
["2017-10-23", 10.39, 10.19, 10.40, 10.15, 1074465.0]]
arr_1 = np.array(data)
print(arr_1)
print(type(arr_1))