ndarray合并非整形数组
时间: 2023-10-01 16:05:54 浏览: 40
可以使用numpy中的concatenate函数来合并非整形数组。该函数可以将多个数组沿着指定的轴连接起来。
示例代码:
```
import numpy as np
a = np.array([1.0, 2.0, 3.0])
b = np.array([4.0, 5.0, 6.0])
c = np.array([7.0, 8.0, 9.0])
result = np.concatenate((a, b, c))
print(result)
```
输出:
```
[1. 2. 3. 4. 5. 6. 7. 8. 9.]
```
在这个例子中,我们创建了三个浮点型数组a、b和c,并使用concatenate函数将它们合并成一个新的数组result。
相关问题
ndarray整形数组变非整形数组
在Python中,可以使用numpy库中的astype()函数来将整形数组转换为非整形数组。例如,将一个整形数组a转换为float类型的数组,可以使用以下代码:
```python
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = a.astype(float)
print(b)
```
输出结果为:
```python
[1. 2. 3. 4. 5.]
```
在astype()函数中,传入目标类型作为参数即可。其他支持的类型包括bool、int、complex等。
ndarray 赋值给 dataframe数组的一列
可以使用 pandas 的 DataFrame 类中的 "assign" 方法或者索引器 "[]" 来将 ndarray 赋值给 DataFrame 的一列。
示例:
```python
import pandas as pd
import numpy as np
# 创建 DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# 创建 ndarray
nd = np.array([7, 8, 9])
# 使用 assign 方法
df = df.assign(C = nd)
# 使用索引器
df['C'] = nd
```
在这个例子中,我们将 ndarray 赋值给了 DataFrame 中名为 "C" 的一列。