pandas 熊猫添加行而不是列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31199710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:33:46  来源:igfitidea点击:

pandas add row instead of column

pythonpandasappend

提问by Pavel M.

I'm new to pandas, but trying to simply add a row

我是Pandas的新手,但试图简单地添加一行

class Security:
    def __init__(self):
        self.structure = ['timestamp', 'open', 'high', 'low', 'close', 'vol']
        self.df = pd.DataFrame(columns=self.structure)  # index =
    def whats_inside(self):
        return self.df
    """
    Some skipped code...
    """
    def add_data(self, timestamp, open, high, low, close, vol):
        data = [timestamp, open, high, low, close, vol]
        self.df = self.df.append (data)

sec = Security()
print sec.whats_inside()
sec.add_data ('2015/06/01', '1', '2', '0.5', '1', '100')
print sec.whats_inside()

but the output is:

但输出是:

            0 close high  low open timestamp  vol
0  2015/06/01   NaN  NaN  NaN  NaN       NaN  NaN
1           1   NaN  NaN  NaN  NaN       NaN  NaN
2           2   NaN  NaN  NaN  NaN       NaN  NaN
3         0.5   NaN  NaN  NaN  NaN       NaN  NaN
4           1   NaN  NaN  NaN  NaN       NaN  NaN
5         100   NaN  NaN  NaN  NaN       NaN  NaN

This means, I'm adding a column instead of row. Yes, I've tried to google but still didnt get the point how do make it simple pythonic way.

这意味着,我要添加一列而不是行。是的,我试过谷歌,但仍然没有明白如何使它简单的pythonic方式。

p.s. I know that's simple, but I'm just missing something important.

ps 我知道这很简单,但我只是遗漏了一些重要的东西。

回答by DeepSpace

There are several ways to add a new row. Perhaps the easiest one is (if you want to add the row to the end) is to use loc:

有多种方法可以添加新行。也许最简单的方法是(如果您想将行添加到最后)是使用loc

df.loc[len(df)] = ['val_a', 'val_b', .... ]

locexpects an index. len(df)will return the number of rows in the dataframe so the new row will be added to the end of the dataframe.

loc期待一个索引。len(df)将返回数据帧中的行数,因此新行将被添加到数据帧的末尾。

'['val_a', 'val_b', .... ]' is a list of values of the row, in the same order of the columns, so the list's length must be equal to the number of columns, otherwise you will get a ValueErrorexception. An exception for this is that if you want all the columns to have the same values you are allowed to have that value as a single element in the list, for example df.loc[len(df)] = ['aa'].

'['val_a', 'val_b', .... ]' 是行值的列表,列的顺序相同,所以列表的长度必须等于列数,否则会得到一个ValueError例外。一个例外是,如果您希望所有列都具有相同的值,则可以将该值作为列表中的单个元素,例如df.loc[len(df)] = ['aa'].

NOTE:a good idea will be to always use reset_indexbefore using this method because if you ever delete a row or work on a filtered dataframe you are not guaranteed that the rows' indexes will be in sync with the number of rows.

注意:一个好主意是reset_index在使用此方法之前始终使用,因为如果您曾经删除过一行或处理过滤过的数据帧,则不能保证行的索引与行数同步。

回答by yangjie

You should append Series or DataFrame. (Series would be more appropriate in your case)

您应该附加 Series 或 DataFrame。(系列更适合您的情况)

import pandas as pd
from pandas import Series, DataFrame
class Security:
    def __init__(self):
        self.structure = ['timestamp', 'open', 'high', 'low', 'close', 'vol']
        self.df = pd.DataFrame(columns=self.structure)  # index =
    def whats_inside(self):
        return self.df
    """
    Some skipped code...
    """
    def add_data(self, timestamp, open, high, low, close, vol):
        data = [timestamp, open, high, low, close, vol]
        # append Series
        self.df = self.df.append(pd.Series(data, index=self.structure), ignore_index=True)
        # or DataFrame
        # self.df = self.df.append(pd.DataFrame([data], columns=self.structure), ignore_index=True)

sec = Security()
print sec.whats_inside()
sec.add_data ('2015/06/01', '1', '2', '0.5', '1', '100')
sec.add_data ('2015/06/02', '1', '2', '0.5', '1', '100')
print sec.whats_inside()

Output:

输出:

    timestamp open high  low close  vol
0  2015/06/01    1    2  0.5     1  100
1  2015/06/02    1    2  0.5     1  100