import pandas as pd data = [[-4, -3, -2, -1], [1, 2, 3, 4]] data2 = [5, 6, 7, 8] data3 = [9, 10, 11, 12] data_list = [] data_list.append(data) print(data_list) data_list.append(data2) print(data_list) data_list.insert(0, data3) print(data_list) print() # add header (title row) using pandas df = pd.DataFrame(data, columns=['A', 'B', 'C', 'D']) print(df, "\n") temp_df = pd.DataFrame([data2], columns=['A', 'B', 'C', 'D']) df = pd.concat([df, temp_df]).reset_index(drop=True) print(df, "\n") temp_df = pd.DataFrame({"A": data3[0], "B": data3[1], 'C': data3[2], 'D': data3[3]}, index=[0]) df = pd.concat([temp_df, df]).reset_index(drop=True) print(df, "\n") # add a new column using pandas size = len(df) data2 = [i * 2 for i in range(size)] temp = pd.Series(data2, name='XYZ') df = df.join(temp) print(df)
Aug 9, 2021
[Python] to append/insert data into a list/array
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment