py pandas 处理Excel表格

import pandas as pd

class Main():
  def __init__(self):

    # 读取excel
    self.df = pd.read_excel("C:\\Users\\ajanuw\\Desktop\\pexcel\\test.xlsx")
    
    # 打印
    print(self.df.head())

    
    # 遍历rows
    for index,row in self.df.iterrows():
      print(index, row['姓名'], row['身高'])
    
    # size
    print( self.df['姓名'].size )

    # 遍历columns
    for name in self.df['姓名']:
      print(name)

    # 添加新的列
    # self.df['other'] = ''
    self.df['other'] = self.df['年龄'] % 2
    print(self.df.head())

Main()

保存excel

import pandas as pd

class Main():
  def __init__(self):
   df = pd.DataFrame( [['a', 'b'],['c', 'd']],index=['row 1', 'row 2'],columns=['col 1', 'col 2'])
   df.to_excel("./output.xlsx")  

Main()
posted @ 2020-11-20 17:03  Ajanuw  阅读(164)  评论(0编辑  收藏  举报