Pandas - DataFrame

class pandas.DataFrame(data=Noneindex=Nonecolumns=Nonedtype=Nonecopy=False)

范例

d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d) # 把字典转换为DataFrame
print(df) 
#   col1  col2
#0     1     3
#1     2     4

print(df.dtypes) # 默认类型为int64
#col1    int64
#col2    int64
#dtype: object

df = pd.DataFrame(data=d, dtype=np.int8) # 指定类型为int8

>>> df2 = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)), columns=['a', 'b', 'c', 'd', 'e']) # 把numpy ndarray 转换为DataFrame
print(df2)
#    a   b   c   d   e
#0   2   8   8   3   4
#1   4   2   9   0   9
#2   1   0   7   8   0
#3   5   1   7   1   3
#4   6   0   2   4   2

属性

T 转置
at  
axes  
blocks  
columns 返回列标签
dtypes 返回各列的类型
empty 返回是否为空
ftype  
iat  
iloc  
index 返回行标签
ix  
loc  
ndim  
shape  
size  
style  
values  

方法

head(n) 返回头n行数据
tail(n) 返回末n行数据
describe() 返回数据的快速统计汇总
sort(columns) 按标签columns进行排序
append(df) 添加df到尾部
   
   
   
   
   

参考 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html

猜你喜欢

转载自my.oschina.net/cttmayi/blog/1818983