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 df
to 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?
How do you convert an array to a DataFrame in Python? To convert an array to a dataframe with Python you need to 1) have your NumPy array (e.g., np_array), and 2) use the pd. DataFrame() constructor like this: df = pd. DataFrame(np_array, columns=['Column1', 'Column2']) .
Rows and columns of NumPy arrays can be selected or modified using the square-bracket indexing notation in Python. To select a row in a 2D array, use P[i] . For example, P[0] will return the first row of P . To select a column, use P[:, i] .
You can add a NumPy array element by using the append() method of the NumPy module. The values will be appended at the end of the array and a new ndarray will be returned with new and old values as shown above. The axis is an optional integer along which define how the array is going to be displayed.
You can use the df. loc() function to add a row to the end of a pandas DataFrame: #add row to end of DataFrame df.
@BalrogOfMoira is that really faster than simply creating the dataframe to append?
df.append(pd.DataFrame(arr.reshape(1,-1), columns=list(df)), ignore_index=True)
Otherwise @Wonton you could simply concatenate arrays then write to a data frame, which could the be appended to the original data frame.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With