-
创建 DataFrame
from random import randint import pandas as pd import numpy as np df = pd.DataFrame(index=range(10), columns=list('ABCD'))
-
# 动态添加一列
A = np.array(range(20, 30)) df['E'] = A print(df)
-
# 添加一行 注意列名必须一致
# append 生成新的DataFrame,原DataFrame不变 ignore_index 是否继续累加原行号 _11 = pd.DataFrame(data=[[1,1,1,1,1]], columns=list('ABCDE')) print(_11) _11df = df.append(_11, ignore_index=True) print(_11df) _12 = pd.Series({'A':randint(-1,1), 'B':randint(-1,1), 'C':randint(-1,1), 'D':randint(-1,1), 'E':randint(-1,1)}) _12df = df.append(_12, ignore_index=True) print(_12df) # 可以用做添加一行(前提是没有10的行号),如果有第10的行号,就是修改这行值 df.loc[10] = [randint(-1,1) for n in range(5)] print(df)
-
# 判断 24 < df['E']列 < 28 的值
# TODO 记得加括号哦 print(df[(df['E'] > 24) & (df['E'] < 28)])
-
# 修改值
df.loc[6, :] = [66,66,66,66,66] df.loc[[7,8,9], :] = [[77,77,77,77,77], [88,88,88,88,88], [99,99,99,99,99]] print(df)
-
# 获取 A 列值为 nan 的行
print(df[pd.isna(df['A'])]['A'])
-
# 获取 A 列值不为 nan 的行
print(df[~pd.isna(df['A'])]['A'])
-
# 获取整个 df 是缺测的
isna = pd.isna(df) print(isna) # 获取缺测的 index indnan = np.where(isna == False) # 因为where查找的下标是二维数组,所以先把df转为numpy, 再通过二维下标,获取值 ddf = np.array(df) print(ddf[indnan])
-
# 按行转为一维
print(ddf.flatten())