Series
创建
import pandas as pd
import numpy as np
c1 = pd.Series(np.arange(4), index=list("abcd"))
c2 = pd.Series({"name": "ccb", "age": 22, "tel": "10086"})
复制代码
切片
c2.index
c2.values
c2["name"]
c2[[1, 2]]
c2[["name", "tel"]]
复制代码
DataFrame
创建
df = pd.DataFrame(np.arange(12).reshape(3, 4), index=list("abc"), columns=list("wxyz"))
复制代码
# 效果
w x y z
a 0 1 2 3
b 4 5 6 7
c 8 9 10 11
复制代码
切片
df[:1]
df[:2]
df["x"]
df.index=["a","c"]
df.set_index("a")
df.set_index(["a","c"])
s1["a","b"]
df.loc["a"].["b"]
df.swaplevel()
复制代码
s = df.loc["a":"b", :]
s1 = df.loc[["a", "c"], "w"]
s2 = df.loc["b", ["x", "y"]]
复制代码
m1 = df.iloc[0:2, :]
m2 = df.iloc[[1,2],[0,2]]
复制代码
import pandas as pd
dn = pd.read_csv("./dogNames2.csv")
uu = dn[(dn["Row_Labels"].str.len() > 4) & (dn["Count_AnimalName"] > 700)]
print(uu)
复制代码
方法
df.info()
df.values
df.index
df.columns
df.shape
df.shape[0]
df.shape[1]
df.head(3)
df.tail(3)
df["w"].max()
df["w"].min()
df["w"].median()
df["w"].argmax()
df["w"].argmin()
df.sort_values(by="age", ascending=False)
复制代码
缺失数据处理
pd.isnull(t)
pd.notnull(t)
复制代码
t["w"].fillna(t["w"].mean())
复制代码