I have a pandas dataframe with 10 rows and 5 columns and a numpy matrix of zeros np.zeros((10,3)).

I want to concat the numpy matrix to the pandas dataframe but I want to delete the last column from the pandas dataframe before concatenating the numpy array to it.

So I will end up with a matrix of 10 rows and 5 - 1 + 3 = 7 columns.

I guess I could use

new_dataframe = pd.concat([
    original_dataframe,
    pd.DataFrame(np.zeros((10, 3)), dtype=np.int)
], axis=1, ignore_index=True)

where original_dataframe has 10 rows and 5 columns.

How do I delete the last column from original_dataframe before concatenating the numpy array? And how do I make sure I preserve all the data types?

share
2  
you can slice the original df new_dataframe = pd.concat([original_dataframe.ix[:, :-1], pd.DataFrame(np.zeros((10, 3)), dtype=np.int)], axis=1, ignore_index=True) with regards to your last comment aren't the datatypes preserved anyway? – EdChum Sep 26 '16 at 8:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.