0

I want to create a new column called 'test' in my dataframe that is equal to the sum of all the columns starting from column number 9 to the end of the dataframe. These columns are all datatype float.

Below is the code I tried but it didn't work --> gives me back all NaN values in 'test' column:

df_UBSrepscomp['test'] = df_UBSrepscomp.iloc[:, 9:].sum()
PineNuts0
  • 4,740
  • 21
  • 67
  • 112

1 Answers1

0

If I'm understanding your question, you want the row-wise sum starting at column 9. I believe you want .sum(axis=1). See an example below using column 2 instead of 9 for readability.

df = DataFrame(npr.rand(10, 5))
df.iloc[0:3, 0:4] = np.nan # throw in some na values
df.loc[:, 'test'] = df.iloc[:, 2:].sum(axis=1); print(df)
         0        1        2        3        4     test
0      NaN      NaN      NaN      NaN  0.73046  0.73046
1      NaN      NaN      NaN      NaN  0.79060  0.79060
2      NaN      NaN      NaN      NaN  0.53859  0.53859
3  0.97469  0.60224  0.90022  0.45015  0.52246  1.87283
4  0.84111  0.52958  0.71513  0.17180  0.34494  1.23187
5  0.21991  0.10479  0.60755  0.79287  0.11051  1.51094
6  0.64966  0.53332  0.76289  0.38522  0.92313  2.07124
7  0.40139  0.41158  0.30072  0.09303  0.37026  0.76401
8  0.59258  0.06255  0.43663  0.52148  0.62933  1.58744
9  0.12762  0.01651  0.09622  0.30517  0.78018  1.18156
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235