[英]Pandas: "ValueError: Buffer has wrong number of dimensions (expected 1, got 0)
I am trying to create new columns in dataframe based on arithmetic on existing columns. 我试图基于现有列的算术在数据框中创建新列。 The following works fine: 以下工作正常:
df_2016 = pd.DataFrame(np.random.rand(10,1), columns=['current'])
df_future = pd.DataFrame(columns={'2017e', '2018e'})
i = 1
inflation = 0.02
for column in df_future:
df_future[column] = df_2016 *(1+inflation)**i
i = i + 1
But when I try to create a longer sequence of columns: 但是当我尝试创建更长的列序列时:
df_future2 = 'bl2021 bl2022 bl2023 bl2024 bl2025 bl2026 bl2027 bl2028 bl2029 bl2030 bl2031 bl2032 bl2033 bl2034 bl2035 bl2036 bl2037 bl2038 bl2038 \
bl2039 bl2040 bl2041 bl2042 bl2043 bl2044 bl2045 bl2046 bl2047 bl2048 bl2049 bl2050'
df_future2 = df_future2.split()
df_future2 = pd.DataFrame(columns=df_future2)
for column in df_future2:
df_future2[column] = df_2016 * (1 + inflation) ** i
i = i + 1
I get: 我得到:
ValueError: Buffer has wrong number of dimensions (expected 1, got 0)
I don't get it. 我不明白 Any clever thoughts? 有什么聪明的想法吗?
You don't have to preallocate the data frame. 您不必预先分配数据帧。 Simply add the columns as you go: 只需添加列即可:
future2 = 'bl2021 bl2022 bl2023 bl2024 bl2025 bl2026 bl2027 bl2028 bl2029 bl2030 bl2031 bl2032 bl2033 bl2034 bl2035 bl2036 bl2037 bl2038 bl2038 \
bl2039 bl2040 bl2041 bl2042 bl2043 bl2044 bl2045 bl2046 bl2047 bl2048 bl2049 bl2050'
column_names = future2.split()
df_future2 = pd.DataFrame()
for column in column_names:
df_future2[column] = df_2016 * (1 + inflation) ** i
i = i + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.