简体   繁体   English

Pandas(Python)-将列表附加到具有列名称的空数据框的问题

[英]Pandas (Python) - Problem appending list to empty dataframe with column names

I am trying to append a list looking like this 我正在尝试附加一个看起来像这样的列表

myList = ['2018-01-12', 'MMM', 'BUY', 42, 236.5229]

to an empty dataframe (with "header" / columns names). 到一个空的数据框(带有“标题” /列名称)。

To create the dataframe I've done the following: 要创建数据框,我做了以下工作:

tradeLog = pd.DataFrame(columns=["DATE", "TICKER", "ORDER_TYPE", "AMOUNT", "PRICE"])

I am trying to append the list as a row in the following way: 我正在尝试通过以下方式将列表追加为一行:

tradeLog.append(myList, ignore_index=True)

(NOTICE: My goal i to iterate over some data - a lot of lists in the same format - and the add them one by one to the dataframe) (注意:我的目标是迭代一些数据-许多相同格式的列表-并将它们一个接一个地添加到数据框中)

The pandas documentation reads 熊猫文档阅读

DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None) DataFrame.append(其他,ignore_index = False,verify_integrity = False,sort = None)

other : DataFrame or Series/dict-like object, or list of these The data to append. 其他:DataFrame或类似Series / dict的对象,或这些对象的列表要附加的数据。

So you need to have to transform your list prior to appending it to your DataFrame: 因此,在将列表追加到DataFrame之前,您需要对其进行转换:

something that might work is to zip the list of your columns to the content of your myList so it would be: 可能myList是将列的列表压缩到myList的内容上,这样就可以了:

tradeLog = pd.DataFrame(columns=["DATE", "TICKER", "ORDER_TYPE", "AMOUNT", "PRICE"])
myList = ['2018-01-12', 'MMM', 'BUY', 42, 236.5229]
myDict = dict(zip(tradeLog.columns.tolist(), myList))

tradeLog.append(myDict, ignore_index=True)

or tradeLog.append(pd.DataFrame(myDict), ignore_index=True) tradeLog.append(pd.DataFrame(myDict), ignore_index=True)

This being said you need to ensure your lists are always the same length as your columns names list. 这就是说,您需要确保列表的长度始终与列名列表相同。

DataFrame.append() is for appending rows from an other pandas dataframe or series (see the docs ). DataFrame.append()用于附加其他熊猫数据框或系列的行 (请参阅docs )。

So, if it is absolutely necessary to do this line by line, you can 因此,如果绝对有必要逐行执行此操作,则可以

tradeLog = tradeLog.append(pd.Series(myList, index=tradeLog.columns), ignore_index=True)

(Nb: tradeLog.loc[len(tradeLog)] = ... appends to the end only as long as you have a simple integer index on tradeLog , but might break for more complex use cases.) (Nb: tradeLog.loc[len(tradeLog)] = ...仅在tradeLog上具有简单的整数索引时才追加到末尾,但是对于更复杂的用例可能会中断。)

You might also want to consider this remark from the docs: 您可能还需要考虑以下文档中的这一说法:

Iteratively appending rows to a DataFrame can be more computationally intensive than a single concatenate. 迭代地将行添加到DataFrame可能比单个连接更多地占用大量计算资源。 A better solution is to append those rows to a list and then concatenate the list with the original DataFrame all at once. 更好的解决方案是将这些行添加到列表中,然后一次将列表与原始DataFrame连接起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM