Derrick Sherrill
Derrick Sherrill

Reputation: 96

Dataframe reverse for drop(column = )

I'm trying to manipulate a dataframe using a cumsum function.

My data looks like this:

To perform my cumsum, I use

df = pd.read_excel(excel_sheet, sheet_name='Sheet1').drop(columns=['Material']) # Dropping material column

I run the rest of my code, and get my expected outcome of a dataframe cumsum without the material listed:

df2 = df.as_matrix() #Specifying Array format
new = df2.cumsum(axis=1)
print(new)

However, at the end, I need to replace this material column. I'm unsure how to use the add function to get this back to the beginning of the dataframe.

Upvotes: 1

Views: 1064

Answers (1)

sacuL
sacuL

Reputation: 51395

IIUC, then you can just set the material column to the index, then do your cumsum, and put it back in at the end:

df2 = df.set_index('Material').cumsum(1).reset_index()

An alternative would be to do your cumsum on all but the first column:

df.iloc[:,1:] = df.iloc[:,1:].cumsum(1)

Upvotes: 2

Related Questions