机器学习—DateFrame

#pandas之DataFrame的学习
import pandas as pd
import numpy as np
'创建一个DF'
t=pd.DataFrame(np.arange(12).reshape((3,4)))
#   0  1   2   3
#0  0  1   2   3
#1  4  5   6   7
#2  8  9  10  11
'改变索引'
t1=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'
d1={"name":["xiao","xiaoo"],"age":[20,23],"tel":[10089,10009]}
pd.DataFrame(d1)
#    name  age    tel
#0   xiao   20  10089
#1  xiaoo   23  10009

'列表创建DF'
d2=[{"name":"xiaom","age":12,"tel":1234},{"name":"xiaomm","age":13,"tel":13567}]
pd.DataFrame(d2)
#     name  age    tel
#0   xiaom   12   1234
#1  xiaomm   13  13567

'数据的读取'
df=pd.read_csv('E:\PYCHAR\新建文件夹 (2)\chipotle.csv',seq='\t')

'将数据集存入一个名为data的DataFrame对象中'
df=pd.DataFrame(df)
df
      order_id  ...  item_price
0            1  ...      $2.39 
1            1  ...      $3.39 
2            1  ...      $3.39 
3            1  ...      $2.39 
4            2  ...     $16.98 
        ...  ...         ...
4617      1833  ...     $11.75 
4618      1833  ...     $11.75 
'查看数据行列信息'
df.index
RangeIndex(start=0, stop=4622, step=1)

df.columns
Index(['order_id', 'quantity', 'item_name', 'choice_description',
       'item_price'],
      dtype='object')
'展示数据类型'
df.dtypes

order_id               int64
quantity               int64
item_name             object
choice_description    object
item_price            object
dtype: object
'显示数据前几行,默认为5行,可以自己设置行数'
df.head()
   order_id  ...  item_price
0         1  ...      $2.39 
1         1  ...      $3.39 
2         1  ...      $3.39 
3         1  ...      $2.39 
4         2  ...     $16.98 
[5 rows x 5 columns]

'显示后几行,默认为5,可以自己设置'
df.tail()
      order_id  ...  item_price
4617      1833  ...     $11.75 
4618      1833  ...     $11.75 
4619      1834  ...     $11.25 
4620      1834  ...      $8.75 
4621      1834  ...      $8.75 
[5 rows x 5 columns]

'展示数据详细信息'
df.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4622 entries, 0 to 4621
Data columns (total 5 columns):
 #   Column              Non-Null Count  Dtype 
---  ------              --------------  ----- 
 0   order_id            4622 non-null   int64 
 1   quantity            4622 non-null   int64 
 2   item_name           4622 non-null   object
 3   choice_description  3376 non-null   object
 4   item_price          4622 non-null   object
'快速统计'
df.describe()
          order_id     quantity
count  4622.000000  4622.000000
mean    927.254868     1.075725
std     528.890796     0.410186
min       1.000000     1.000000
25%     477.250000     1.000000
50%     926.000000     1.000000
75%    1393.000000     1.000000
max    1834.000000    15.000000

'按照什么标签值进行排序'
df.sort_values(by="order_id")
      order_id  ...  item_price
0            1  ...      $2.39 
1            1  ...      $3.39 
2            1  ...      $3.39 
3            1  ...      $2.39 
4            2  ...     $16.98 
        ...  ...         ...
4617      1833  ...     $11.75 
4618      1833  ...     $11.75 
4620      1834  ...      $8.75 
4619      1834  ...     $11.25 
4621      1834  ...      $8.75 
[4622 rows x 5 columns]

'取前20行,方括号中为数字'
df[:20]
'取列,方括号中为字符串'
df["order_id"]
'同时取行和取列'
df[:20]["order_id"]
t1
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
######t1.loc[]是通过标签来获取数据
'取某一行某一列'
t1.loc["a","w"]
0
t1.loc[["a","c"],["w","y"]]
   w   y
a  0   2
c  8  10
'取行的时候冒号加不加都可以'
t1.loc["a",:]
w    0
x    1
y    2
z    3
t1.loc[["a","c"],:]
   w  x   y   z
a  0  1   2   3
c  8  9  10  11
'取某一列'
t1.loc[:,"y"]
a     2
b     6
c    10

######t1.iloc[]是通过位置来获取数据
t1
   w  x   y   z
a  0  1   2   3
b  4  5   6   7
c  8  9  10  11
'取第一行'
t1.iloc[1,:]
w    4
x    5
y    6
z    7
'[[行,列],[行,列]]'
t1.iloc[[0,2],[2,1]]
    y  x
a   2  1
c  10  9
'赋值'
t1.iloc[[0,2],[2,1]]=200
t1
   w    x    y   z
a  0  200  200   3
b  4    5    6   7
c  8  200  200  11

'找出一个标签中符合条件的项'
df[(10<df["order_id"])&(df["order_id"]<20)]
'判断是否含有NAN'
pd.isnull(t1)
       w      x      y      z
a  False  False  False  False
b  False  False  False  False
c  False  False  False  False
pd.notnull(t1)
      w     x     y     z
a  True  True  True  True
b  True  True  True  True
c  True  True  True  True
'选择w列中不含NAN的行'
t1[pd.notnull(t1["w"])]
   w    x    y   z
a  0  200  200   3
b  4    5    6   7
c  8  200  200  11
'删除有NAN的行,axis=0表示行'
t1.dropna(axis=0)
   w    x    y   z
a  0  200  200   3
b  4    5    6   7
c  8  200  200  11
'当其中行含有NAN,则删除整行'
t1.dropna(axis=0,how="any")
   w    x    y   z
a  0  200  200   3
b  4    5    6   7
c  8  200  200  11
'当其中行不全含有NAN,则不删除整行'
t1.dropna(axis=0,how="all")
   w    x    y   z
a  0  200  200   3
b  4    5    6   7
c  8  200  200  11
d2=[{'name': 'xiaom', 'age': 12, 'tel': 1234}, {'name': 'xiaomm', 'tel': 13567}, {'name': 'huahua', 'age': 10}]
d3=pd.DataFrame(d2)

d3
     name   age      tel
0   xiaom  12.0   1234.0
1  xiaomm   NaN  13567.0
2  huahua  10.0      NaN
'NAN中填充0'
d3.fillna(0)
     name   age      tel
0   xiaom  12.0   1234.0
1  xiaomm   0.0  13567.0
2  huahua  10.0      0.0
'NAN中填充均值'
d3.fillna(d3.mean)
'每一列设置均值填充'
d3
     name   age      tel
0   xiaom  12.0   1234.0
1  xiaomm   NaN  13567.0
2  huahua  12.0      NaN
d3["age"].mean()
12.0

d3["age"]=d3["age"].fillna(d3["age"].mean())

d3
     name   age      tel
0   xiaom  12.0   1234.0
1  xiaomm  12.0  13567.0
2  huahua  12.0      NaN

'怎么设置一个值为nan'
t.loc["D","W"]=np.nan

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值