将行附加到 Pandas DataFrame 添加 0 列 [英] Appending row to Pandas DataFrame adds 0 column

查看:56
本文介绍了将行附加到 Pandas DataFrame 添加 0 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 Pandas DataFrame 来存储数据.不幸的是,我无法提前知道我将拥有的数据行数.所以我的方法如下.

I'm creating a Pandas DataFrame to store data. Unfortunately, I can't know the number of rows of data that I'll have ahead of time. So my approach has been the following.

首先,我声明一个空的 DataFrame.

First, I declare an empty DataFrame.

df = DataFrame(columns=['col1', 'col2'])

然后,我附加一行缺失值.

Then, I append a row of missing values.

df = df.append([None] * 2, ignore_index=True)

最后,我可以一次向这个 DataFrame 一个单元格中插入值.(为什么我必须一次做一个单元格是一个很长的故事.)

Finally, I can insert values into this DataFrame one cell at a time. (Why I have to do this one cell at a time is a long story.)

df['col1'][0] = 3.28

这种方法非常有效,除了 append 语句向我的 DataFrame 插入一个额外的列.在过程结束时,我在输入 df 时看到的输出看起来像这样(有 100 行数据).

This approach works perfectly fine, with the exception that the append statement inserts an additional column to my DataFrame. At the end of the process the output I see when I type df looks like this (with 100 rows of data).

<class 'pandas.core.frame.DataFrame'>
Data columns (total 2 columns):
0            0  non-null values
col1         100  non-null values
col2         100  non-null values

df.head() 看起来像这样.

      0   col1   col2
0  None   3.28      1
1  None      1      0
2  None      1      0
3  None      1      0
4  None      1      1

对导致此 0 列出现在我的 DataFrame 中的原因有任何想法吗?

Any thoughts on what is causing this 0 column to appear in my DataFrame?

推荐答案

追加正在尝试将一列追加到您的数据框.它尝试追加的列未命名,其中有两个 None/Nan 元素,pandas 将(默认情况下)命名为名为 0 的列.

The append is trying to append a column to your dataframe. The column it is trying to append is not named and has two None/Nan elements in it which pandas will name (by default) as column named 0.

为了成功执行此操作,数据框的附加列名称必须与当前数据框的列名称一致,否则将创建新列(默认情况下)

In order to do this successfully, the column names coming into the append for the data frame must be consistent with the current data frame column names or else new columns will be created (by default)

#you need to explicitly name the columns of the incoming parameter in the append statement
df = DataFrame(columns=['col1', 'col2'])
print df.append(Series([None]*2, index=['col1','col2']), ignore_index=True)


#as an aside

df = DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])
dfRowImproper = [1,2,3,4]
#dfRowProper = DataFrame(arange(4)+1,columns=['A','B','C','D']) #will not work!!! because arange returns a vector, whereas DataFrame expect a matrix/array#
dfRowProper = DataFrame([arange(4)+1],columns=['A','B','C','D']) #will work


print df.append(dfRowImproper) #will make the 0 named column with 4 additional rows defined on this column

print df.append(dfRowProper) #will work as you would like as the column names are consistent

print df.append(DataFrame(np.random.randn(1,4))) #will define four additional columns to the df with 4 additional rows


print df.append(Series(dfRow,index=['A','B','C','D']), ignore_index=True) #works as you want

这篇关于将行附加到 Pandas DataFrame 添加 0 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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