Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and add the last line of a python pandas data frame on to itself with updated index

Tags:

python

pandas

I have a dataframe such as:


2013-07 114.60 89.62 125.64

2013-08 111.55 88.63 121.57

2013-09 108.31 86.24 117.93


index is YY-MM date series I would like to copy and add the last row to the original dataframe with a new updated index. The new dataframe should look like:


2013-07 114.60 89.62 125.64

2013-08 111.55 88.63 121.57

2013-09 108.31 86.24 117.93

2013-10 108.31 86.24 117.93


how can I do this?

like image 884
Cenk T Avatar asked Oct 04 '13 15:10

Cenk T


People also ask

How do you append to the end of a data frame?

The append() method appends a DataFrame-like object at the end of the current DataFrame. The append() method returns a new DataFrame object, no changes are done with the original DataFrame.

How do I get the last row of a pandas DataFrame?

Select & print last row of dataframe using tail() It will return the last row of dataframe as a dataframe object. Using the tail() function, we fetched the last row of dataframe as a dataframe and then just printed it.

How do you get the last index of a DataFrame in Python?

iloc – Pandas Dataframe. iloc is used to retrieve data by specifying its index. In python negative index starts from the end so we can access the last element of the dataframe by specifying its index to -1.

How do you update data in a DataFrame in Python?

Pandas DataFrame update() Method The update() method updates a DataFrame with elements from another similar object (like another DataFrame). Note: this method does NOT return a new DataFrame. The updating is done to the original DataFrame.


1 Answers

This is how I parsed your data (easy, but you really should have code snippets describing the data in your question):

In [1]: df = pd.read_csv('in.txt', index_col=0, sep=' ', header=None, parse_dates=[0])

In [2]: df
Out[2]: 
                 1      2       3                                                                                                                                                             
0                                                                                                                                                                                             
2013-07-01  114.60  89.62  125.64                                                                                                                                                             
2013-08-01  111.55  88.63  121.57                                                                                                                                                             
2013-09-01  108.31  86.24  117.93

Now, using concat/append and slicing, you can re-add the last row under a new date with:

In [3]: new_date = pd.datetools.to_datetime('2013-10')

In [3]: new_data = pd.DataFrame(df[-1:].values, index=[new_date], columns=df.columns)

In [4]: df = df.append(new_data)

In [5]: df
Out[5]: 
                 1      2       3                                                                                                                                                                                                   
2013-07-01  114.60  89.62  125.64                                                                                                                                                                                                   
2013-08-01  111.55  88.63  121.57                                                                                                                                                                                                   
2013-09-01  108.31  86.24  117.93                                                                                                                                                                                                   
2013-10-01  108.31  86.24  117.93  

Note, however, that adding data row by row is not the recommended way - it is better to do appends on lower-level structures, such as lists and dicts (which are faster at individual appends), and convert the data to a DataFrame at bulk when you actually need to analyse it.

like image 179
metakermit Avatar answered Sep 30 '22 12:09

metakermit