<jupyter_start><jupyter_text>GoalLearn how to add and deleting rows in `pandas.DataFrame`. import pandas as pdimport numpy as npfrom pandas import (Series, Index, DataFrame) df.loc, df.ix, df.iloc --> and how it works with the index<jupyter_code>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<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>df.loc[max(df.index) + 1]=[4, 'Andy','cherise',4 ] 
# need to let data_widget know to update based on change in df

df<jupyter_output><empty_output><jupyter_text>How to insert a row in an arbitrary position?<jupyter_code># replaces not inserts
df.iloc[1] = [5, 'Raymond','apple',4 ] 
df<jupyter_output><empty_output><jupyter_text>* insert at end and reindex?<jupyter_code># 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<jupyter_output><empty_output><jupyter_text>How to insert a row in an arbitrary position Deleting rows  -> by computing the label corresponding to row number...<jupyter_code># 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
# 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
# modify
df.loc[1,'name'] = 'Ludwig'
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16220313289540172, 'fraction_numerical': 0.015159171298635674, 'mean_word_length': 5.85121107266436, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '288852', 'license': 'Apache-2.0', 'n_tokens_mistral': 768, 'n_tokens_neox': 738, 'n_words': 178, 'path': 'notebooks/pandas.DataFrame.adding_and_deleting.rows.ipynb', 'repo_name': 'rdhyee/webtech-learning', 'size': '1979'}