sfotiadis
sfotiadis

Reputation: 977

Add row with duplicate index in a panda dataframe

Let's say you have a data frame:

df = pd.DataFrame(columns = ['item'], index = ['datetime'])

You can add an item on a specific date index:

df.loc[pd.datetime(2015, 1, 15)] = 23

Is there any way I can add/append new items on the same index?

Disclaimer: I understand that the index is supposed to be unique and what I'm asking is not very panda-stic. But for some applications, especially with multiple indexes it provides an easy way to select chunks of data.

EDIT: Meanwhile I've found the append() function and it seems to do exactly that although it's kinda cumbersome. Look also here.

Upvotes: 2

Views: 7803

Answers (3)

Ali Taheri
Ali Taheri

Reputation: 146

I have tried many ways to do it, and the easiest with minimal error is to create a dataframe same as what you already have then use pandas.concat([maindata, add_data]) to push the "add_data" into the "maindata". Even if you have a duplicated indexes, it still will add the new row "add_data" to your main dataframe "maindata". try the below code.

import pandas as pd    
maindata = pd.DataFrame([[12, 13, 15], [200, 300, 400]], index=['serial1', 'serial2'], columns=['HP No', 'Company', 'Name'])
    add_data = pd.DataFrame([[5000, 6000, 7000]], index=['serial1'], columns=['HP No', 'Company', 'Name'])
    maindata = pd.concat([maindata, add_data])

I hope that it solves the issue. in case you want to have a professional way of sorting duplicated indexes, you can try to read about sort_index(inplace=True). GL

Upvotes: 1

sfotiadis
sfotiadis

Reputation: 977

Meanwhile I've found the append() function and it seems to do exactly that although it's kinda cumbersome. Look also here.

Upvotes: 0

alacy
alacy

Reputation: 5074

You could try:

df.groupby(df.index).sum()

This would group the rows with duplicate indices and then sum them up.

Upvotes: 4

Related Questions