将新列添加到 Pandas DataFrame 时的 NaN 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26221300/
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
NaN values when new column added to pandas DataFrame
提问by gtnbz2nyt
I'm trying to generate a new column in a pandas DataFrame that equals values in another pandas DataFrame. When I attempt to create the new column I just get NaNs for the new column values.
我正在尝试在 Pandas DataFrame 中生成一个新列,该列等于另一个 Pandas DataFrame 中的值。当我尝试创建新列时,我只会获得新列值的 NaN。
First I use an API call to get some data, and the 'mydata' DataFrame is one column of data indexed by dates
首先,我使用 API 调用来获取一些数据,“mydata”DataFrame 是按日期索引的一列数据
mydata = Quandl.get(["YAHOO/INDEX_MXX.4"],
trim_start="2001-04-01", trim_end="2014-03-31",
collapse="monthly")
The next DataFrame I get from a CSV with the following code, and it contains many columns of data with the same number of rows as 'mydata'
我使用以下代码从 CSV 中获取的下一个 DataFrame,它包含许多行数与“mydata”相同的数据列
DWDATA = pandas.DataFrame.from_csv("filename",
header=0,
sep=',',
index_col=0,
parse_dates=True,
infer_datetime_format=True)
I then try to generate the new column like this:
然后我尝试像这样生成新列:
DWDATA['MXX'] = mydata.iloc[:,0]
Again, I just get NaN values. Can someone help me understand why it's doing this and how to resolve? From what I've read it looks like I might have something wrong with my indexes. The indexes are dates in each DataFrame, but 'mydata' have end-of-month dates while 'DWDATA' has beginning-of-month dates.
同样,我只得到 NaN 值。有人可以帮助我理解为什么这样做以及如何解决吗?从我读过的内容来看,我的索引可能有问题。索引是每个 DataFrame 中的日期,但 'mydata' 具有月末日期,而 'DWDATA' 具有月初日期。
回答by gtnbz2nyt
Because the indexes are not exactly equal, NaNs will result. Either one or both of the indexes must be changed to match. Example:
因为索引不完全相等,所以会产生 NaN。必须更改其中一个或两个索引以匹配。例子:
mydata = mydata.set_index(DWDATA.index)
The above will change the index of the 'mydata' DataFrame to match the index of the 'DWDATA' DataFrame.
以上将更改“mydata”DataFrame 的索引以匹配“DWDATA”DataFrame 的索引。
Since the number of rows are exactly equal for the two DataFrames, you can also just pass the values of 'mydata' to the new 'DWDATA' column:
由于两个 DataFrame 的行数完全相等,您也可以将 'mydata' 的值传递给新的 'DWDATA' 列:
DWDATA['MXX'] = mydata.iloc[:,0].values

