限制Excel行的数量

时间:2017-10-23 04:14:50

标签: python pandas

举个例子:

import pandas as pd

def multiple_dfs(sheet, row=0):

    writer = pd.ExcelWriter("testing.xlsx", engine='openpyxl')
    df1 = pd.DataFrame(['Title'])
    df2 = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice', 'Robert', 'John', 'Joe', 'Jordan'], 
                       'income': [40000, 50000, 42000, 25000, 2000000, 12000, 32000]})

    df1.to_excel(writer, sheet, startrow=row, header=None, \
                 index=False)
    df2.to_excel(writer, sheet, startrow=row+2, index=False)

    writer.save()
    writer.close()

    style_dfs(df1)

multiple_dfs('aaa')

对于dfs,我想将Excel行数限制为5.

是否可以用熊猫做这样的事情?

1 个答案:

答案 0 :(得分:1)

要限制导出到Excel的行数,可以使用pandas.Dataframe.head(),如:

df.head(5).to_excel(...)