How to add row to DataFrame with time stamp index in Pandas?
Adding row to DataFrame with time stamp index:
import pandas as pd df = pd.DataFrame(columns=['Name', 'Age']) df.loc['2014-05-01 18:47:05', 'Name'] = 'Rocky' df.loc['2014-05-01 18:47:05', 'Age'] = 21 df.loc['2014-05-02 18:47:05', 'Name'] = 'Sunny' df.loc['2014-05-02 18:47:05', 'Age'] = 22 df.loc['2014-05-03 18:47:05', 'Name'] = 'Mark' df.loc['2014-05-03 18:47:05', 'Age'] = 25 print("\n------------ BEFORE ----------------\n") print(df) line = pd.to_datetime("2014-05-01 18:50:05", format="%Y-%m-%d %H:%M:%S") new_row = pd.DataFrame([['Bunny', 26]], columns=['Name', 'Age'], index=[line]) df = pd.concat([df, pd.DataFrame(new_row)], ignore_index=False) print("\n------------ AFTER ----------------\n") print(df)
C:\pandas>python example36.py ------------ BEFORE ---------------- Name Age 2014-05-01 18:47:05 Rocky 21 2014-05-02 18:47:05 Sunny 22 2014-05-03 18:47:05 Mark 25 ------------ AFTER ---------------- Name Age 2014-05-01 18:47:05 Rocky 21 2014-05-02 18:47:05 Sunny 22 2014-05-03 18:47:05 Mark 25 2014-05-01 18:50:05 Bunny 26 C:\pandas>pep8 example36.py C:\pandas>
2018-10-11T09:49:21+05:30
2018-10-11T09:49:21+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution