[英]How to add Column vector to Data Frame (Python, Pandas)
I have a df with 365 rows × 8 columns
.我有一个 df
365 rows × 8 columns
。
Columns are the following ['tave', 'tmax', 'tmin', 'vp', 'rhmax', 'rhmin', 'pp', 'gust']
列如下
['tave', 'tmax', 'tmin', 'vp', 'rhmax', 'rhmin', 'pp', 'gust']
So there are 365 measures of 'tave'
during the year.因此,一年中有 365 个
'tave'
。
I want to add new column called 'next day tave'
in the beginning of df.我想在 df 的开头添加名为
'next day tave'
的新列。
How to do it?怎么做?
My attempt is the following:我的尝试如下:
# let's add new column with "next day tave"
# "next day tave" is the value of next day
# e.g row 0, tave=-13.535, next tave=-5.791
next_tave_val = df.values[1:, 0:1]
print(next_tave_val.shape)
(364, 1)
# "next day tave" is 364 and we want to add that column to df that is 365 rows
# so need to add one more value to make it 365
# to append as 2d array
next_tave_val = np.append(next_tave_val, [[0]], axis=0)
print(next_tave_val.shape)
# now let's add 'next tave' column to df
df = df.insert(0,'next tave', next_tave_val)
print(df.shape)
Returns an error:返回错误:
AttributeError: 'NoneType' object has no attribute 'shape'
What did I do wrong?我做错了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.