Python Pandas.DataFrame对Excel操作

对原文的精简 感谢原文博主

https://blog.csdn.net/fengqiaoxian/article/details/80415354

  • 读入: 输出是dataframe
  • import pandas as pd
    excelFile = r’TEMP.xlsx’
    df = pd.DataFrame(pd.read_excel(excelFile))
    print(df)

  • 读取指定sheet
    def read_excel(io, sheet_name=0, header=0, skiprows=None, skip_footer=0,
    index_col=None, names=None, usecols=None, parse_dates=False,
    date_parser=None, na_values=None, thousands=None,
    convert_float=True, converters=None, dtype=None,
    true_values=None, false_values=None, engine=None,
    squeeze=False, **kwds):

  • 选择特定列:考虑输出是dataframe 用其列columns特性
    df1= df[[‘Project Name’, ‘Status’, ‘Offer’, ‘Tax Rate’, ‘PM’]]

  • 定位满足特定要求数据 同MATLAB下find的感觉
    df2 = df1.loc[df1[‘PM’] == ‘Bob’]

  • 同时满足多个要求
    -df2 = df1.loc[df1[‘PM’] == ‘Bob’].loc[df1[‘Status’] == ‘Completed’]
    -获取特定列的值
    pmList = df1[[‘PM’]].values.T.tolist()[:][0] 和前面选特定列有啥不一样?

  • 写入Excel表格
    先转为dataframe 格式 再存 filePath是存放路径
    summaryDataFrame = pd.DataFrame(sum_list)
    summaryDataFrame.to_excel(filePath, encoding=’utf-8’, index=False, header=False)

你可能感兴趣的:(Python)