Goal

Learn how to add and deleting rows in pandas.DataFrame.

import pandas as pd import numpy as np from pandas import (Series, Index, DataFrame)

df.loc, df.ix, df.iloc --> and how it works with the index


In [2]:
columns = ['id', 'name','color','marbles']

data = [

    {'id':0, 'name': 'Fred', 'color':'red', 'marbles':2},
    {'id':1, 'name': 'Zhang', 'color':'blue', 'marbles':5},
    {'id':2, 'name': 'Deb', 'color':'orange', 'marbles':0}
]

df = DataFrame(data, columns=columns)
df


Out[2]:
id name color marbles
0 0 Fred red 2
1 1 Zhang blue 5
2 2 Deb orange 0

Let's keep the index integer (to keep with convention of qgrid)

Can use .loc and an integer not already in the index to insert a row at the end of the DataFrame.


In [3]:
df.loc[max(df.index) + 1]=[4, 'Andy','cherise',4 ] 
# need to let data_widget know to update based on change in df

df


Out[3]:
id name color marbles
0 0 Fred red 2
1 1 Zhang blue 5
2 2 Deb orange 0
3 4 Andy cherise 4

How to insert a row in an arbitrary position?


In [4]:
# replaces not inserts
df.iloc[1] = [5, 'Raymond','apple',4 ] 
df


Out[4]:
id name color marbles
0 0 Fred red 2
1 5 Raymond apple 4
2 2 Deb orange 0
3 4 Andy cherise 4
  • insert at end and reindex?

In [5]:
# http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.DataFrame.reindex.html

new_id = max(df.index) + 1
new_index = list(df.index.copy())

df.loc[new_id]=[4, 'Josh','crab apple',89 ] 

# move new_index to row

row = 1

new_index.insert(row,new_id)
df = df.reindex(new_index)
df


Out[5]:
id name color marbles
0 0 Fred red 2
4 4 Josh crab apple 89
1 5 Raymond apple 4
2 2 Deb orange 0
3 4 Andy cherise 4

How to insert a row in an arbitrary position

Deleting rows -> by computing the label corresponding to row number...


In [6]:
# another approach

line = DataFrame([[90, 'Wolfgang','fuji apple',33 ]], columns=columns, index=[3])
df = pd.concat([df.iloc[:2], line, df.iloc[3:]]).reset_index(drop=True)
df


Out[6]:
id name color marbles
0 0 Fred red 2
1 4 Josh crab apple 89
2 90 Wolfgang fuji apple 33
3 2 Deb orange 0
4 4 Andy cherise 4

In [7]:
# removing rows
# http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html

df = df.drop(df.index[1]).reset_index(drop=True)
df


Out[7]:
id name color marbles
0 0 Fred red 2
1 90 Wolfgang fuji apple 33
2 2 Deb orange 0
3 4 Andy cherise 4

In [10]:
# modify
df.loc[1,'name'] = 'Ludwig'
df


Out[10]:
id name color marbles
0 0 Fred red 2
1 90 Ludwig fuji apple 33
2 2 Deb orange 0
3 4 Andy cherise 4

In [ ]: