mport numpy as np
import pandas as pd
# iloc 主要用於索引取值
df = pd.DataFrame(np.arange(20).reshape(5, 4), index=list('ABCDE'), columns=list('wxyz'))
print(df)
# 取指定行
print(df.head(2))
print(df[1:2])
print(df.tail(4))
# 統計列數
print(df.columns.size)
# 統計數據行數
print(len(df))
# 打印行名
print(df.index)
# 打印列名
print(df.columns)
# 指定行數
print(df.describe().iloc[3])
# 指定某列
print(df['z'])
# 指定某行
print(df.loc['C'])
# 切片所有行,指定列
print(df.loc[:,['x','y']])
# 切片指定行,指定列
print(df.loc[['A','D'],['w','z']])
# 連續切片 iloc 不連續用loc
print(df.iloc[2:4,2:4])
# 根據數字比較 只打印大於2的數字
print(df[df>2])
# 根據列名比較 只打印大於2的行數
print(df[df.x>5])
# 復制數組
df2 = df.copy()
print(df2)
#表顯示滿足條件:指定列x中的值包含'5'和'13'的所有行
print(df2[df2['x'].isin(['5','13'])])
# 計算列平均值
print(df.mean())
# 計算指定列中數字出現的個數
print(df['x'].value_counts())
轉:https://blog.csdn.net/tanlangqie/article/details/78656588