美文网首页
6 Pandas 读取数据

6 Pandas 读取数据

作者: avyhlj | 来源:发表于2020-08-09 10:25 被阅读0次

    利用pandas读取多种格式数据
    1,读取excel文件

    df_excel = pd.read_excel(file_path, sheetname='Sheet1')
    df_excel.info
    

    2,读取csv(',')文件

    df_csv = pd.read_csv(file_path)
    df_csv.info
    

    3,读取txt('\t')文件

    df_txt = pd.read_csv(file_path,sep='\t',header=None,names=['a','b','c'])
    #names给数据列命名
    

    4,读取mysql数据库数据

    import pymysql
    conn = pymysql.connect(host='***',
                           user='***',
                           password='***',
                           db='***',
                           charset='utf8mb4')
    df = pd.read_sql(sql,con=conn)
    

    利用游标进行读取mysql数据

    def fetch_data_db(sql):
        
        connection = pymysql.connect(host='***',
                                     user='***',
                                     password='***',
                                     db='***',
                                     charset='utf8mb4',
                                     port='***',
                                     cursorclass=pymysql.cursors.DictCursor,
                                     # 可以指定返回的类型,指定为queryset的字典类型,比较方便
                                     connect_timeout=86400)
    
        try:
            with connection.cursor() as cursor:
                # Read a single record
                print(sql + ' is running')
                cursor.execute(sql)
                result = cursor.fetchall()
                result = pd.DataFrame(result)
                
        except ValueError:
            print("Error: Oopus, No Data, Drink a coffee")
            return None
    
        finally:
            connection.close()
            print(sql + 'is obtained')
            
        return result
    

    相关文章

      网友评论

          本文标题:6 Pandas 读取数据

          本文链接:https://www.haomeiwen.com/subject/inkldktx.html