简体   繁体   English

熊猫:“ ValueError:缓冲区的维数错误(预期1,得到0)

[英]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.

相关问题 Pandas ValueError:缓冲区的维数错误(预期为 1,得到 2) - Pandas ValueError: Buffer has wrong number of dimensions (expected 1, got 2) ValueError:如果在语句中,则缓冲区的维数错误(预期为1,得到2) - ValueError: Buffer has wrong number of dimensions (expected 1, got 2) on if in statement Cython ValueError:缓冲区的维数错误(预期2,得3) - Cython ValueError: Buffer has wrong number of dimensions (expected 2, got 3) 在创建 pandas DataFrame ValueError: Buffer has wrong number of dimensions (expected 1, got 2) - during creation of pandas DataFrame ValueError: Buffer has wrong number of dimensions (expected 1, got 2) 缓冲区的维数错误(预期为 1,得到 2) - Buffer has wrong number of dimensions (expected 1, got 2) ValueError:使用Seaborn时,缓冲区的维数错误(预期为1,为2) - ValueError: Buffer has wrong number of dimensions (expected 1, got 2) when using seaborn df.loc - ValueError:缓冲区的维数错误(预期为 1,得到 0) - df.loc - ValueError: Buffer has wrong number of dimensions (expected 1, got 0) pd.insert ValueError:缓冲区的维数错误(预期为 1,得到 2) - pd.insert ValueError: Buffer has wrong number of dimensions (expected 1, got 2) pandas:转换数据帧集合时,缓冲区的维数错误(预期为1,得0) - pandas: Buffer has wrong number of dimensions (expected 1, got 0) when transforming a dataframe column of sets Pandas 合并给出错误“缓冲区的维数错误(预期 1,得到 2)” - Pandas merge giving error "Buffer has wrong number of dimensions (expected 1, got 2)"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM