Appending new rows to DataFrames
When performing a data analysis, it is far more common to create new columns than new rows. This is because a new row of data usually represents a new observation and, as an analyst, it is typically not your job to continually capture new data. Data capture is usually left to other platforms like relational database management systems. Nevertheless, it is a necessary feature to know as it will crop up from time to time.
Getting ready
In this recipe, we will begin by appending rows to a small dataset with the .loc
indexer and then transition to using the append
method.
How to do it...
- Read in the names dataset, and output it:
>>> names = pd.read_csv('data/names.csv') >>> names
- Let's create a list that contains some new data and use the
.loc
indexer to set a single row label equal to this new data:
>>> new_data_list = ['Aria', 1] >>> names.loc[4] = new_data_list >>> names
- The
.loc
indexer uses labels to refer to the rows...