python使用备忘(一)

  1. 程序包
    python程序包下载地址:
    http://www.lfd.uci.edu/~gohlke/pythonlibs/
    也可通过pip命令安装

    机器学习相关包:Pandas、NumPy、 SciPy、sklearn
    绘图包:matplotlib

    pandas学习网址
    http://pandas.pydata.org/pandas-docs/stable/
    sklearn学习网址:
    http://scikit-learn.org/stable/

  2. 使用过程遇到的问题

    1. 读文件错误

      OSError: Initializing from file failed
      python读文件默认不支持中文

    2. 字符串转时间格式

      data['time']=pd.to_datetime(data['time'],format='%Y-%m-%d %H:%M:%S')
    3. 数据中存在空值

      ValueError: Input contains NaN, infinity or a value too large fordtype('float32')
      

      判断是否为空

      np.isnan(mat.any()) #and gets False
      np.isfinite(mat.all()) #and gets True

      去除空值

      _train_data.dropna(inplace=True)# drop含有NA的行

      填充上一个值

      data_tmp.fillna(method=’pad’, inplace=True)#method按需修改

      dataframe转list

      df.values.tolist()

      dataframe中字符串分割并转成数值型计算

      data.col1 = data.col2.apply(lambda x:std([float(item) for item in x.split(‘:’)]))

    Windows下读文件pandas.read_csv(path)报错:

    OSError: Initializing from file failed

    原因python3.6默认的是‘uft-8’编码,改到window编码即可。

    sys._enablelegacywindowsfsencoding()

    查看编码

    sys.getfilesystemencoding()

猜你喜欢

转载自blog.csdn.net/u011799895/article/details/76577777
今日推荐