По документу
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html
Свойство loc используется для доступа к группе строк и столбцов по меткам или логическому массиву. Можно написать короткии код, использующий df.loc[index , label]
(.env) boris@boris-All-Series:~/DATAFRAMES$ cat InsertRowFrame1.py
import pandas as pd
df = pd.DataFrame(columns=['Name', 'Age'])
df.loc['2021-05-01 18:47:05', 'Name'] = 'Rick'
df.loc['2021-05-01 18:47:05', 'Age'] = 31
df.loc['2021-05-02 18:47:05', 'Name'] = 'Vivian'
df.loc['2021-05-02 18:47:05', 'Age'] = 34
df.loc['2021-05-03 18:47:05', 'Name'] = 'Djon'
df.loc['2021-05-03 18:47:05', 'Age'] = 36
print("\n************** BEFORE ROW INSERT *****************\n")
print(df)
line = pd.to_datetime("2022-06-01 19:40:05", format="%Y-%m-%d %H:%M:%S")
new_row = pd.DataFrame([['Robert', 37]], columns=['Name', 'Age'], index=[line])
df = pd.concat([df, pd.DataFrame(new_row)], ignore_index=False)
print("\n************* AFTER ROW INSERT *******************\n")
print(df)
(.env) boris@boris-All-Series:~/DATAFRAMES$ python3 InsertRowFrame1.py
************** BEFORE ROW INSERT *****************
Name Age
2021-05-01 18:47:05 Rick 31
2021-05-02 18:47:05 Vivian 34
2021-05-03 18:47:05 Djon 36
************* AFTER ROW INSERT *******************
Name Age
2021-05-01 18:47:05 Rick 31
2021-05-02 18:47:05 Vivian 34
2021-05-03 18:47:05 Djon 36
2022-06-01 19:40:05 Robert 37
No comments:
Post a Comment