我像这样制作数据帧.
df = pd.DataFrame({
'class' : ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
'number' : [1,2,3,4,5,1,2,3,4,5],
'math' : [90, 20, 50, 30, 57, 67, 89, 79, 45, 23],
'english' : [40, 21, 68, 89, 90, 87, 89, 54, 21, 23]
})
Run Code Online (Sandbox Code Playgroud)
我想通过使用一些pandas方法将索引转换为此.(例如set_index,stack ,,,)
df1 = pd.DataFrame(np.random.randint(1, 100, (5, 4)),
columns = [['A', 'A', 'B', 'B'],['english', 'math', 'english', 'math']],
index = [1, 2, 3, 4, 5])
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
如下所示,我创建实例并添加到切片。我想保留实例的内存地址,但事实并非如此。
type Person struct {
name string
}
func main() {
p := Person{name: "foo"}
ps1 := []Person{p}
ps2 := []Person{p}
fmt.Printf("%p\n", &p) // 0xc000010250
fmt.Printf("%p\n", &ps1[0]) // 0xc000010260
fmt.Printf("%p\n", &ps2[0]) // 0xc000010270
}
Run Code Online (Sandbox Code Playgroud)
我知道如果我使用指针和指针切片是可能的。
type Person struct {
name string
}
func main() {
p := Person{name: "foo"}
ps1 := []*Person{&p}
ps2 := []*Person{&p}
fmt.Printf("%p\n", &p) // 0xc00010a210
fmt.Printf("%p\n", ps1[0]) // 0xc00010a210
fmt.Printf("%p\n", ps2[0]) // 0xc00010a210
}
Run Code Online (Sandbox Code Playgroud)
但是,我想将 ps1 和 ps2 设置为 []Person 类型,因为后面的方法的参数类型(不在此处)。有什么办法吗?