[英]Add A 1-D Numpy Array to DataFrame as a Row
Is there a function which allows you to efficiently append a NumPy array directly to a DataFrame?是否有一个 function 可以让您有效地将 append 一个 NumPy 数组直接转换为 ZBA834Z9C112A9A3EB87845
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?我如何直接 append arr
到df
的末尾来得到这个?
+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
| 756 | 123 | 452 |
+------+------+------+
I've tried using df.append(arr)
but it doesn't accept NumPy arrays.我试过使用df.append(arr)
但它不接受 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.我可以将 NumPy 数组转换为 DataFrame 然后 append 它,但我认为这将非常低效,尤其是在数百万次迭代中。 Is there a more efficient way to do it?有没有更有效的方法来做到这一点?
@BalrogOfMoira is that really faster than simply creating the dataframe to append? @BalrogOfMoira 真的比简单地创建 dataframe 到 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.否则@Wonton,您可以简单地连接 arrays 然后写入数据帧,该数据帧可以附加到原始数据帧。
This will work:这将起作用:
df.append(pd.DataFrame(arr).T)
@rafaelc comment can work only if your Pandas DataFrame is indexed from 0 to len(df)-1, so it is not a general workaround and it can easily produce a silent bug in your code. @rafaelc 注释只有在您的 Pandas DataFrame 的索引从 0 到 len(df)-1 时才有效,因此它不是一般的解决方法,它很容易在您的代码中产生无声的错误。
If you are sure that your Numpy array has the same columns of your Pandas DataFrame you could try using the append function with a dict comprehension as follows: If you are sure that your Numpy array has the same columns of your Pandas DataFrame you could try using the append function with a dict comprehension as follows:
data_to_append = {}
for i in range(len(df.columns)):
data_to_append[df.columns[i]] = arr[i]
df = df.append(data_to_append, ignore_index = True)
You need to reassign the DataFrame because append
function does not support in-place modification.您需要重新分配 DataFrame 因为append
function 不支持就地修改。
I hope it helps.我希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.