Is there a function which allows you to efficiently append a NumPy array directly to a DataFrame?
Variables:
df = pd.DataFrame(columns=['col1', 'col2', 'col3'])
Out[1]: +------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| | | |
+------+------+------+
arr = np.empty(3)
# array is populated with values. Random numbers are chosen in this example,
# but in my program, the numbers are not arbitrary.
arr[0] = 756
arr[1] = 123
arr[2] = 452
Out[2]: array([756, 123, 452])
How do I directly append arr to the end of dfto get this?
+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| 756 | 123 | 452 |
+------+------+------+
I've tried using df.append(arr) but it doesn't accept NumPy arrays. I could convert the NumPy array into a DataFrame then append it, but I think that would be very inefficient, especially over millions of iterations. Is there a more efficient way to do it?
df.loc[len(df)] = arr