将新列添加到pandas DataFrame时的NaN值 [英] NaN values when new column added to pandas DataFrame
问题描述
我正在尝试在pandas DataFrame中生成一个新列,该列等于另一个pandas DataFrame中的值.当我尝试创建新列时,我只是获得新列值的NaN.
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.
首先,我使用API调用来获取一些数据,"mydata" DataFrame是按日期索引的一列数据
First I use an API call to get some data, and the 'mydata' DataFrame is one column of data indexed by dates
mydata = Quandl.get(["YAHOO/INDEX_MXX.4"],
trim_start="2001-04-01", trim_end="2014-03-31",
collapse="monthly")
我使用以下代码从CSV中获得的下一个DataFrame,它包含多列数据,这些列的行数与'mydata'相同
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'
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]
同样,我只得到NaN值.有人可以帮助我了解为什么这样做以及如何解决吗?从我阅读的内容来看,我的索引可能有问题.索引是每个DataFrame中的日期,但是"mydata"具有月末日期,而"DWDATA"具有月末日期.
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.必须更改一个或两个索引以使其匹配.示例:
Because the indexes are not exactly equal, NaNs will result. Either one or both of the indexes must be changed to match. Example:
mydata = mydata.set_index(DWDATA.index)
以上内容将更改"mydata"数据框的索引以匹配"DWDATA"数据框的索引.
The above will change the index of the 'mydata' DataFrame to match the index of the 'DWDATA' DataFrame.
由于两个DataFrame的行数完全相等,因此您也可以将'mydata'的值传递给新的'DWDATA'列:
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:
DWDATA['MXX'] = mydata.iloc[:,0].values
这篇关于将新列添加到pandas DataFrame时的NaN值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!