df = pd.DataFrame(columns=['0','90','180','270']) contrast = graycoprops(glcm, 'contrast') dissimilarity = graycoprops(glcm, 'dissimilarity') homogeneity = graycoprops(glcm, 'homogeneity') energy = graycoprops(glcm, 'energy') correlation = graycoprops(glcm, 'correlation') asm = graycoprops(glcm, 'ASM') 将下面的六个1*4的矩阵赋值给df的每一行,每一行允许自定义索引
时间: 2023-12-26 18:03:50 浏览: 63
您可以使用 `loc` 方法将这些矩阵赋值给 DataFrame 的每一行,并自定义索引。下面是一个示例:
```python
import pandas as pd
import numpy as np
df = pd.DataFrame(columns=['0', '90', '180', '270'])
contrast = np.array([1, 2, 3, 4])
dissimilarity = np.array([5, 6, 7, 8])
homogeneity = np.array([9, 10, 11, 12])
energy = np.array([13, 14, 15, 16])
correlation = np.array([17, 18, 19, 20])
asm = np.array([21, 22, 23, 24])
df.loc[0] = contrast
df.loc[1] = dissimilarity
df.loc[2] = homogeneity
df.loc[3] = energy
df.loc[4] = correlation
df.loc[5] = asm
df.index = ['contrast', 'dissimilarity', 'homogeneity', 'energy', 'correlation', 'asm']
print(df)
```
这将输出:
```
0 90 180 270
contrast 1 2 3 4
dissimilarity 5 6 7 8
homogeneity 9 10 11 12
energy 13 14 15 16
correlation 17 18 19 20
asm 21 22 23 24
```
在上述示例中,首先创建一个空的 DataFrame,并定义了索引列。然后,使用 `loc` 方法将每个矩阵赋值给 DataFrame 的每一行,并使用自定义的索引值。最后,使用 `index` 属性将索引列设置为自定义的索引。
阅读全文