python|pandas常见函数积累

shape()

返回数组或者数据框有多少行或者多少列

import numpy as np
x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]])
#输出数组的行和列数
print x.shape  #结果: (4, 3)
#只输出行数
print x.shape[0] #结果: 4
#只输出列数
print x.shape[1] #结果: 3

因此可以用来遍历行或者列

#计算每列的均值
ex=np.array(np.mean(x[:,i]) for i in range(x.shape[1]))

reshpae()

reshape()是数组array中的方法,作用是将数据重新组织

a = np.array([[1,2,3,4],[5,6,7,8]])  #二维数组
print(a.shape[0])  #值为2,最外层矩阵有2个元素,2个元素还是矩阵。
print(a.shape[1])  #值为4,内层矩阵有4个元素。
b= np.array([1,2,3,4,5,6,7,8])  
b.reshape(2,4)
print(b)
#array([[1,2,3,4],
[5,6,7,8]])

pd.Dataframe.columns

返回数据框的列名

pd.Dataframe.columns.values

返回数据框的的列值

[[]]

我之前想提取两列,哈哈,想半天,最后看了一个同学给的demo
直接pd.[["列名","列名"]]

还是见的太少了

_

就是常见的命名规则,

这里指代损失函数
# Create centroids with kmeans for 2 clusters
cluster_centers,_ = kmeans(fifa[scaled_features], 2)

unique()

去重函数,默认是行去重

[]

# Leave this list as is
number_cols = ['HP', 'Attack', 'Defense']

# Remove the feature without variance from this list
non_number_cols = ['Name', 'Type', 'Legendary']

# Create a new dataframe by subselecting the chosen features
df_selected = pokemon_df[number_cols + non_number_cols]


<script.py> output:
       HP  Attack  Defense                   Name   Type  Legendary
    0  45      49       49              Bulbasaur  Grass      False
    1  60      62       63                Ivysaur  Grass      False
    2  80      82       83               Venusaur  Grass      False
    3  80     100      123  VenusaurMega Venusaur  Grass      False
    4  39      52       43             Charmander   Fire      False

比如这个栗子,可以用来提取子数据框

format

print("{} rows in test set vs. {} in training set. {} Features.".format(X_test.shape[0], X_train.shape[0], X_test.shape[1]))

输出保留一位百分比小数的结果

print("{0:.1%} accuracy on test set.".format(acc)) 

isnull()

判断是否有缺失值

返回bool

.sum()

除了求和之外还有判断个数此时等同于count

pd.isnull.sum()

.dtypes

DataFrame.dtypes
返回DataFrame中的dtypes
这将返回一个Series,其中包含每列的数据类型。结果的索引是原始DataFrame的列。具有混合类型的列与objectdtype 一起存储

1.type() 返回参数的数据类型

2.dtype 返回数组中元素的数据类型

3.astype() 对数据类型进行转换

value_counts()

value_counts()是一种查看表格某列中有多少个不同值的快捷方法,并计算每个不同值有在该列中有多少重复值。
所以就是统计

In [3]: volunteer["category_desc"].value_counts()
Out[3]: 
Strengthening Communities    307
Helping Neighbors in Need    119
Education                     92
Health                        52
Environment                   32
Emergency Preparedness        15
Name: category_desc, dtype: int64

apply

我先放个栗子,后面继续补充这个函数,感觉做一些简单的处理很好用

# Create a list of the columns to average
run_columns = ["run1", "run2", "run3", "run4", "run5"]

# Use apply to create a mean column
running_times_5k["mean"] = running_times_5k.apply(lambda row: row[run_columns].mean(), axis=1)

# Take a look at the results
print(running_times_5k)
script.py> output:
          name  run1  run2  run3  run4  run5   mean
    0      Sue  20.1  18.5  19.6  20.3  18.3  19.36
    1     Mark  16.5  17.1  16.9  17.6  17.3  17.08
    2     Sean  23.5  25.1  25.2  24.6  23.9  24.46
    3     Erin  21.7  21.1  20.9  22.1  22.2  21.60
    4    Jenny  25.8  27.1  26.1  26.7  26.9  26.52
    5  Russell  30.9  29.6  31.4  30.4  29.9  30.44
原文地址:https://www.cnblogs.com/gaowenxingxing/p/12295272.html