import pandas as pd
"""
pandas doc:
df.dtypes 查看数据每column 数据类型
id int64
x0 float64
df.reindex 查看多少行多少列的数据结构 [569 rows x 21 columns]>
df.reindex_axis 查看数据行列带数据
df.info() 查看数据每列non-null 情况
df.head(1).values 索引查看前一行数据.默认取前五行
[[ 1.33000e+02 4.49512e-01 nan 4.13178e-01 3.03781e-01
-1.23848e-01 -1.84227e-01 -2.19076e-01 2.68537e-01 1.59960e-02
-7.89267e-01 -3.37360e-01 -7.28193e-01 -4.42587e-01 -2.72757e-01
-6.08018e-01 -5.77235e-01 -5.01126e-01 1.43371e-01 -4.66431e-01
-5.54102e-01]]
......
......
df.tail(1) 查看最后一行数据
df[n:n+k].values取出二维数组查看第n到n+k行数据
df转numpy
df.to_numpy()[:2]取前2行:
[[ 1.330000e+02 4.495120e-01 nan 4.131780e-01 3.037810e-01
-1.238480e-01 -1.842270e-01 -2.190760e-01 2.685370e-01 1.599600e-02
-7.892670e-01 -3.373600e-01 -7.281930e-01 -4.425870e-01 -2.727570e-01
-6.080180e-01 -5.772350e-01 -5.011260e-01 1.433710e-01 -4.664310e-01
-5.541020e-01]
[ 2.730000e+02 -1.245485e+00 -8.423170e-01 -1.255026e+00 nan
-4.263010e-01 -1.088781e+00 -9.763920e-01 -8.988980e-01 9.834960e-01
4.570200e-02 -4.936390e-01 3.486200e-01 -5.524830e-01 -5.268770e-01
2.253098e+00 -8.276200e-01 -7.807390e-01 -3.769970e-01 -3.102390e-01
1.763010e-01]]
......
......
......
df.describe() :数据分析获取每列的数量,均值,标准差,min,max,25位,50位,75位
id x0 ... x18 x19
count 569.000000 5.690000e+02 ... 5.690000e+02 5.690000e+02
mean 284.000000 2.811951e-08 ... -5.272408e-09 1.230228e-08
std 164.400426 1.000880e+00 ... 1.000880e+00 1.000880e+00
min 0.000000 -2.029648e+00 ... -1.532890e+00 -1.096968e+00
25% 142.000000 -6.893850e-01 ... -6.516810e-01 -5.851180e-01
50% 284.000000 -2.150820e-01 ... -2.194300e-01 -2.299400e-01
75% 426.000000 4.693930e-01 ... 3.556920e-01 2.886420e-01
max 568.000000 3.971288e+00 ... 7.071917e+00 9.851593e+00
数据转置:行转列 df.T
df.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')据排序:axis 0行,1 column
默认axis=0 根据列排序也就是索引,如果指定by则按照对应列排序,axis=1时,by根据行填写行名
ascending:布尔型,True则升序,可以是[True,False],即第一字段升序,第二个降序
......
......
数据选取:
df['x0'].values 选取某一列数据
df[:n]前两行数据
或者使用df.loc[0]取索引0行数据
df.loc[0:1]取索引0到1行数据
df.loc[:,"x0":"x3"]:,单独取某列所有行用df.loc[:,"x3"]
取所有行的x0到x3之间的列
只取某几列比如x0,x7 : df.loc[:,["x0","x7"]]
df.iloc[0]取索引一行
df.iloc[0:4] 前三行左闭右开
取列df.iloc: print(df.iloc[:,0:4]) 前三列不支持“列名”
id x0 x1 x2
0 133 0.449512 NaN 0.413178
1 273 -1.245485 -0.842317 -1.255026
2 175 -1.549664 -1.126219 -1.546652
取第一行第二列:
print(df.iloc[1,2]) or print(df.at[0,"x2"])
......
......
缺失值填充:
print(df.fillna(value=5)) NaN
0 133 0.449512 5.000000 0.413178 ... -0.501126 0.143371 -0.466431 -0.554102
1 273 -1.245485 -0.842317 -1.255026 ... -0.780739 -0.376997 -0.310239 0.176301
......
......
均值查看:
print(df.mean()) mean(0)行 1 列
求和列:
df.loc["rows_sum"]=df.apply(lambda x:x.sum())
print(df)
567 2.0 1.579888 0.456187 ... 1.424827 0.237036 0.293559
568 39.0 -0.183840 0.356123 ... 0.133639 -0.819980 -0.229940
rows_sum 161596.0 0.000016 1.247216 ... -0.000005 -0.000003 0.000007
求和行:
df["rows_sum"]=df.apply(lambda x:x.sum(),axis=1)
print(df)
id x0 x1 ... x18 x19 rows_sum
0 133 0.449512 NaN ... -0.466431 -0.554102 128.790148
1 273 -1.245485 -0.842317 ... -0.310239 0.176301 266.205423
2 175 -1.549664 -1.126219 ... 0.795207 -0.149751 160.581688
......
......
"""
df=pd.read_csv('./datasets/breast_a.csv')
df["rows_sum"]=df.apply(lambda x:x.sum(),axis=1)
print(df)
数据列名称修改:
df=pd.read_csv('./datasets/breast_a.csv',index_col=False)
new_df=pd.DataFrame(df)
df1=new_df.rename(columns={"x0":"fid0","x1":"fid1"})
df1.to_csv(path_or_buf='./datasets/breast_a.csv',index=False,encoding="utf-8")
id fid0 fid1 x2 ... x16 x17 x18 x19
0 133 0.449512 NaN 0.413178 ... -0.501126 0.143371 -0.466431 -0.554102
1 273 -1.245485 -0.842317 -1.255026 ... -0.780739 -0.376997 -0.310239 0.176301
2 175 -1.549664 -1.126219 -1.546652 ... -1.057501 -1.913447 0.795207 -0.149751
数据替换某一列
# print(pd.read_csv('./datasets/breast_a.csv',index_col=False))
new_df["x3"].replace(0.303781,9999,inplace=True)
print(new_df.x3)
替换所有:
df.replace(to_replace, value) 前面是需要替换的值,后面是替换后的值。