使用 pandas 在数据框中添加一个空行 [英] Append an empty row in dataframe using pandas

查看:258
本文介绍了使用 pandas 在数据框中添加一个空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在数据帧的末尾添加一个空行,但无法这样做,甚至试图了解熊猫如何通过append函数工作,但仍然没有得到它.

I am trying to append an empty row at the end of dataframe but unable to do so, even trying to understand how pandas work with append function and still not getting it.

代码如下:

import pandas as pd

excel_names = ["ARMANI+EMPORIO+AR0143-book.xlsx"]
excels = [pd.ExcelFile(name) for name in excel_names]
frames = [x.parse(x.sheet_names[0], header=None,index_col=None).dropna(how='all') for x in excels]
for f in frames:
    f.append(0, float('NaN'))
    f.append(2, float('NaN'))

有两列,行数随机.

在for循环中带有"print f",得到这个:

with "print f" in for loop i Get this:

                             0                 1
0                   Brand Name    Emporio Armani
2                 Model number            AR0143
4                  Part Number            AR0143
6                   Item Shape       Rectangular
8   Dial Window Material Type           Mineral
10               Display Type          Analogue
12                 Clasp Type            Buckle
14               Case Material   Stainless steel
16              Case Diameter    31 millimetres
18               Band Material           Leather
20                 Band Length  Women's Standard
22                 Band Colour             Black
24                 Dial Colour             Black
26            Special Features       second-hand
28                    Movement            Quartz

推荐答案

使用pandas.DataFrame.append()添加新的pandas.Series.

Add a new pandas.Series using pandas.DataFrame.append().

如果您希望指定新行的名称(又称索引"),请使用:

If you wish to specify the name (AKA the "index") of the new row, use:

df.append(pandas.Series(name='NameOfNewRow'))

如果您不想命名新行,请使用:

If you don't wish to name the new row, use:

df.append(pandas.Series(), ignore_index=True)

其中df是您的pandas.DataFrame.

where df is your pandas.DataFrame.

这篇关于使用 pandas 在数据框中添加一个空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆