python小知识片段

    xiaoxiao2021-03-25  108

    python 获取当前文件目录: cur_dir = os.path.dirname(os.path.abspath(__file__)) 获取父目录: parent_dir = os.path.dirname(cur_dir) 删除目录model_path: import os,shutil file_list = os.listdir(model_path) for f in file_list: filepath = os.path.join(model_path, f)     if os.path.isfile(filepath): os.remove(filepath)     elif os.path.isdir(filepath):         shutil.rmtree(filepath, True) 创建目录:model_path import os if not os.path.exists(model_path):     os.makedirs(self.model_path) 注意:os.makedirs递归地创建目录树 os.mkdir只创建一级目录,如果上级目录不存在抛出异常 判断目录和文件是否存在: if not os.path.exists(path): return False else:     return True pandas DataFrame http://blog.csdn.net/maliang_1993/article/details/50907983   《pandas.DataFrame 的操作简单经验(创建,索引,增添,删除)》 import pandas as pd pandas DataFrame 转 numpy array: data = pd_data.arrays 用其他的DataFrame创建 df = pd.DataFrame({'ds':pd_data['last_update'],'y':pd_data['co']}) 获得DataFrame的某一列: pd_data['co'] 从多维数组中创建: pd_data = pd.DataFrame(list_data, columns=column_names)  这里list_data为[[],[],[],[]]类型的数据,[]的每个元素代表一行数据 numpy array import numpy as np 获取numpy array的某一列: array[:,2:]获得array的第3列和之后的数据   array[:,2]获得array的第3列数据 多个 array横向合并,形成多列数据 array3 = np.concatenate((array1,array2),axis=1) 日期: 字符串转datetime对象 day_begin = datetime.datetime.strptime(begin_time,"%Y-%m-%d %H:%M:%S") datatime对象转date对象 datetime_data.date() date对象计算天数间隔 (date1 - date2).days datetime对象转字符串 datatime_data.strftime("%Y-%m-%d %H:%M:%S") numpy.datetime64转为datetime对象: pd.to_datetime(str(day_history)).replace(tzinfo=None)
    转载请注明原文地址: https://ju.6miu.com/read-15178.html

    最新回复(0)