Python导入excel数据

1、官网下载并安装pycharm

https://www.jetbrains.com/

Python导入excel数据_第1张图片

2、下载插件pandas

File-->settings-->Project-->Python Interpreter,点击加号,搜索并安装

Python导入excel数据_第2张图片

pandas读取excel时需要依赖库,同理安装上:xlrd模块和xlwt模块。其他模块有提示也同样安装

3、执行代码,导入一个excel查看下

import  pandas as pd

def print_hi():
    # Use a breakpoint in the code line below to debug your script.
   # print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
    df = pd.read_excel(r'C:\Users\12133\Downloads\系统日志 (33).xlsx')
    print(df.head())


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi()

结果有报错,发现版本不对,可以更新版本Python导入excel数据_第3张图片

打开cmd,输入pip install -upgrade openyx1。可以再pycharm的插件里看已更新

Python导入excel数据_第4张图片

再次执行成功,但是字段都没有对应上

Python导入excel数据_第5张图片

4、调整对齐问题

import  pandas as pd

def print_hi():
    # Use a breakpoint in the code line below to debug your script.
   # print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.
    #调整列名显示不对齐问题
    pd.set_option('display.unicode.east_asian_width',True)
    #解决行列显示不全问题,将默认输出的最大行列调整为1000
    pd.set_option('display.max_rows',1000)
    pd.set_option('display.max_columns', 1000)
    df = pd.read_excel(r'C:\Users\12133\Downloads\系统日志 (33).xlsx')
    print(df.head())


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi()

Python导入excel数据_第6张图片

你可能感兴趣的:(Python实践,excel,python,pandas)