[英]Using a numpy array of length less than pandas dataframe to fill in pandas dataframe
df = pd.DataFrame(index=pd.date_range('2017-01-01', '2017-01-10', freq='D'), columns=['test'])
vals = np.array([1.0, 2.0])
df['test'] = vals
I get this error: ValueError: Length of values does not match length of index 我收到此错误:ValueError:值的长度与索引的长度不匹配
Any way I can fix it? 有什么办法可以解决吗? This error is arising because lenght of vals
is less than length of df
. 这个错误产生的原因的lenght vals
小于长度df
。 I want to insert numpy NaNs for the places where I have no values. 我想为我没有值的地方插入numpy NaN。
您可以像这样仅分配第一行:
df['test'][:len(vals)] = vals
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.