pandas 经典操作集锦
代码语言:javascript
复制# -*- coding:utf-8 -*-
# /usr/bin/python
__author__ = 'yanerrol'
import pandas as pd
import numpy as np
print(pd.__version__)
array = [1,2,3,4]
df = pd.DataFrame(array)
print('df :n',df)
# 数组转序列
df_series = pd.Series(array)
print('df_series:n',df_series)
# 字典转序列
d = {'a':1,'b':2,'c':3,'d':4,'e':5}
df = pd.Series(d)
print('df :n',df)
# numpy 创建序列
dates = pd.date_range('today',periods=6) # 定义时间序列作为 index
num_arr = np.random.randn(6,4) # 传入 numpy 随机数组
columns = ['A','B','C','D'] # 将列表作为列名
df = pd.DataFrame(num_arr, index = dates, columns = columns)
print(df)
# 从表格读取序列
# df = pd.read_csv('test.csv', encoding='gbk, sep=';')
# 字典创建df 并设置索引
data = {'animal': ['cat', 'cat', 'snake', 'dog',