1

I have a dataframe enter image description here

I need to group columns with numbers into one column. And make a final dataframe look like this. enter image description here

Can anyone help me with this?

Yerbol
  • 39
  • 6
  • Can you please show what you have tried? – Anurag A S Apr 10 '19 at 06:50
  • You can do this by first dropping the columns you want to exclude from the computation `df.drop(list_of_cols_to_drop, axis=1)` then perform a row-wise sum `df.sum(axis=1)` and finally add the result to the original dataframe. By combining these three steps you'll have the desired effect:`df['sum'] = df.drop(list_of_cols_to_drop, axis=1).sum(axis=1)`. – Djib2011 Apr 10 '19 at 06:57
  • @Djib2011 I don't want them to exclude, I just want to group it to one column :) – Yerbol Apr 10 '19 at 07:11
  • I mean the other columns (reporting_date, id_region, etc.) that you don't want to sum. – Djib2011 Apr 10 '19 at 07:12
  • @Djib2011 How to group them 101, 102... etc to one column? :D – Yerbol Apr 10 '19 at 07:13
  • you want to sum them right? the `df.sum(axis=1)` does exactly that – Djib2011 Apr 10 '19 at 10:12
  • @Djib2011 thanks did by my own way, by using df2 = pd.melt(df2, id_vars=["reporting_date",'ДОХОДЫ, всего'], var_name="id_region", value_name="Sum") – Yerbol Apr 10 '19 at 13:06

0 Answers0