MindJuice MindJuice -4 years ago 211
Python Question

Adding multiple columns in pandas

Just getting familiar with pandas and I'm having an issue with


I have some CSV data that I've munged into the following format:

20161201 9:00:00, 10, 20, 11, 21
20161201 9:00:01, 10, 20, 11, 21
20161201 9:00:02, 10, 20, 11, 21

I want to transform it to the following:

Time, AAA, BBB
20161201 9:00:00, 21, 41
20161201 9:00:01, 21, 41
20161201 9:00:02, 21, 41

I'm using the following:

df.groupby(df.columns, axis=1).sum()

What I get instead is:

AAA, BBB, Time
21, 41, 0.0
21, 41, 0.0
21, 41, 0.0

  1. How can I prevent pandas from summing the Time column? I tried the following:

    df.groupby([c for c in df.columns if c != "Time"], axis=1).sum()

    but that gives the error:

    Grouper for 'AAA' not 1-dimensional

  2. How can I avoid pandas moving the Time column?

If it matters, I combined two pandas DataFrames together to get the above data using:

df = pd.merge(df1, df2, how="outer")

And then renamed some columns to shorten the names so I could group them.

Answer Source

You can set Time as index:

df.set_index("Time").pipe(lambda x: x.groupby(x.columns, axis=1).sum())

enter image description here

Call reset_index later, if you want it to be a column in the result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download