简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM