将两个numpy.ndarray数据转组合成DataFrame数据
时间: 2024-04-24 08:25:48
浏览: 184
要将两个NumPy数组组合成DataFrame数据,你可以使用`pandas`库。假设你有两个数组`array1`和`array2`,你可以按照以下方式将它们组合成DataFrame:
```python
import pandas as pd
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array(['a', 'b', 'c'])
df = pd.DataFrame({'Column1': array1, 'Column2': array2})
这将创建一个DataFrame`df`,其中包含两列:'Column1'和'Column2'。'Column1'列的值来自`array1`,而'Column2'列的值来自`array2`。在这个例子中,DataFrame将为:
Column1 Column2
0 1 a
1 2 b
2 3 c
你可以根据需要调整列名和数组的维度来适应你的数据。
相关问题
多个numpy.ndarray转换成一个dataframe
### 回答1:
可以使用 pandas 库将多个 numpy.ndarray 转换成一个 dataframe。具体的做法是,先将每个 numpy.ndarray 转换成一个 pandas 的 Series,再将这些 Series 合并成一个 dataframe。
下面是示例代码:
```python
import pandas as pd
import numpy as np
# 假设有两个 numpy.ndarray,分别为 arr1 和 arr2
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# 将每个 ndarray 转换成一个 Series
s1 = pd.Series(arr1)
s2 = pd.Series(arr2)
# 将两个 Series 合并成一个 dataframe
df = pd.concat([s1, s2], axis=1)
# 打印输出结果
print(df)
```
numpy.ndarray保存为桌面excel
最低
0.47元/天
开通会员,查看完整答案

成为会员后, 你将解锁


相关推荐

















