赞
踩
列1 | 列2 | 列3 | 列4 |
import pandas as pd | |||
s = pd.Series([1, 3, 5, np.nan, 6, 8]) | |||
pd.DataFrame({'A': [1, 2, 3]}) | |||
dates = pd.date_range("20130101", periods=6) | |||
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list("ABCD")) | |||
df2 = pd.DataFrame( | |||
...: { | |||
...: "A": 1.0, | |||
...: "B": pd.Timestamp("20130102"), | |||
...: "C": pd.Series(1, index=list(range(4)), dtype="float32"), | |||
...: "D": np.array([3] * 4, dtype="int32"), | |||
...: "E": pd.Categorical(["test", "train", "test", "train"]), | |||
...: "F": "foo", | |||
...: } | |||
查看数据 | df.head() | 头几行 | |
df.tail(3) | 尾3行 | ||
df2.dtypes | 类型 | ||
df.index | 索引 行 | ||
df.columns | 列名 | ||
df.to_numpy() | numpy格式 | ||
df.describe() | 显示数据的快速统计摘要 | A B C D | |
count 6.000000 6.000000 6.000000 6.000000 | |||
mean 0.073711 -0.431125 -0.687758 -0.233103 | |||
std 0.843157 0.922818 0.779887 0.973118 | |||
min -0.861849 -2.104569 -1.509059 -1.135632 | |||
25% -0.611510 -0.600794 -1.368714 -1.076610 | |||
50% 0.022070 -0.228039 -0.767252 -0.386188 | |||
75% 0.658444 0.041933 -0.034326 0.461706 | |||
max 1.212112 0.567020 0.276232 1.071804 | |||
df.T | 转置 | ||
df.sort_index(axis=1, ascending=False) | 排序 | ||
df.sort_values(by="B") | 排充 | ||
选择 | df["A"] | 【列名】 | |
df[0:3] | 0-3行 | ||
df["20130102":"20130104"] | 行号02-04 | ||
按标签选择 | df.loc[dates[0]] | ||
df.loc[:, ["A", "B"]] | 【行,列】 | ||
df.loc["20130102":"20130104", ["A", "B"]] | |||
df.loc[dates[0], "A"] | 选择具体值 | ||
df.at[dates[0], "A"] | 选择具体值 | ||
按位置选择 | df.iloc[3] | 【行】 | |
df.iloc[3:5, 0:2] | 【行,列】 | ||
df.iloc[[1, 2, 4], [0, 2]] | |||
df.iloc[1:3, :] | |||
df.iloc[:, 1:3] | |||
df.iloc[1, 1] | |||
df.iat[1, 1] | |||
按条件选择 | df[df["A"] > 0] | ||
df[df > 0] | |||
赋值 | df2 = df.copy() | ||
df2["E"] = ["one", "one", "two", "three", "four", "three"] | |||
s1 = pd.Series([1, 2, 3, 4, 5, 6], index=pd.date_range("20130102", periods=6)) | |||
df.at[dates[0], "A"] = 0 | |||
df.iat[0, 1] = 0 | |||
df.loc[:, "D"] = np.array([5] * len(df)) | |||
df2[df2 > 0] = -df2 | |||
df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + ["E"]) | |||
df1.loc[dates[0] : dates[1], "E"] = 1 | |||
df1.dropna(how="any") | |||
df1.fillna(value=5) | |||
pd.isna(df1) | |||
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。