I am trying to create new columns in dataframe based on arithmetic on existing columns. The following works fine:
我尝试在基于现有列的算术基础上创建dataframe中的新列。以下工作正常:
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?
我不明白。任何聪明的想法吗?
1 个解决方案
#1
1
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
#1
1
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