Python 创建与另一个维度相同的空数据框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23195250/
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
Create empty Dataframe with same dimensions as another?
提问by Orvar Korvar
I have searched a bit, but can not find a good answer. I want to create an empty dataframe with same dimensions as another dataframe so I can add new columns. Today I create an empty dataframe filled with zeroes, and then I delete the zero column. I hope there is a better way, but can not find the answer. Can someone help me?
我搜索了一下,但找不到好的答案。我想创建一个与另一个数据框具有相同维度的空数据框,以便我可以添加新列。今天我创建了一个填充零的空数据框,然后我删除了零列。我希望有更好的方法,但找不到答案。有人能帮我吗?
I do like this today and it works, but it is very ugly.
我今天确实喜欢这个并且它有效,但它非常丑陋。
df_copy = pandas.DataFrame(numpy.zeros(len(df_original.index)))
df_copy = df_copy.drop([0],axis=1)
And now I can add new columns as I process data. So basically I want an empty dataframe with same dimensions as another dataframe.
现在我可以在处理数据时添加新列。所以基本上我想要一个与另一个数据帧具有相同维度的空数据帧。
df_copy["price"] = pricesList
df_copy["size"] = sizesList
EDIT: Another closely related question: how do I create an empty Dataframe with dimensions mxn? I have got the answer below how to create an empty dataframe with dimensions 1xn, which is by setting the index. But how do I create an empty nxm dataframe filled with zeroes? The reason I am asking, is because I suspect(?) it is faster to create a zero filled dataframe, and then replace each element as needed. The alternative is to create an empty dataframe with dimensions 1xn and then add columns as needed - which I am told is slow. So it might be faster to create an empty dataframe with nxm dimensions and then replace elements as needed (by copying a list to each column). Say a column has 100 rows, and I create a sublist with 25 rows, so I just copy this list to the correct subcolumn, and repeat. This is faster than adding a new column?
编辑:另一个密切相关的问题:如何创建一个尺寸为 mxn 的空数据框?我在下面得到了如何创建维度为 1xn 的空数据框的答案,这是通过设置索引。但是如何创建一个填充零的空 nxm 数据帧?我问的原因是因为我怀疑(?)创建一个零填充的数据框会更快,然后根据需要替换每个元素。另一种方法是创建一个维度为 1xn 的空数据框,然后根据需要添加列 - 我被告知这很慢。因此,创建一个具有 nxm 维度的空数据框然后根据需要替换元素(通过将列表复制到每列)可能会更快。假设一列有 100 行,我创建了一个 25 行的子列表,所以我只需将此列表复制到正确的子列,然后重复。这比添加新列更快?
采纳答案by kadee
Creating an empty dataframe with the same index and columns as another dataframe:
创建一个与另一个数据帧具有相同索引和列的空数据帧:
import pandas as pd
df_copy = pd.DataFrame().reindex_like(df_original)
回答by Gaspare Bonventre
import pandas as pd
df_copy = pd.DataFrame(index=df_original.index,columns=df_original.columns)
回答by Milind R
@GaspareBonventre's answercan be slow, because of an issuewith the Pandas DataFrame constructor. I find it much faster to do
@GaspareBonventre 的回答可能很慢,因为Pandas DataFrame 构造函数存在问题。我发现这样做要快得多
import numpy as np
df_copy = pd.DataFrame(np.zeros(df_original.shape))
df_copy.index = df_original.index
df_copy.columns = df_original.columns
回答by Ben Saunders
For anyone coming to this page looking to create a dataframe of same columns, same dtypes, and no rows:
对于来到此页面希望创建相同列、相同数据类型且无行的数据框的任何人:
import pandas as pd
df_copy = df_original.iloc[0:0,:].copy()