创建一个以 letters = [A,B] 和 numbers = list(range(6))为索引,值为随机数据的多重索引 DataFrame;为多重索引设置列名称;多重索引分组求和;行列名称转换。
时间: 2024-05-15 15:17:10 浏览: 127
好的,可以的。下面是您想要的回答:
1. 创建一个以 letters = [A,B] 和 numbers = list(range(6))为索引,值为随机数据的多重索引 DataFrame:
```python
import numpy as np
import pandas as pd
letters = ['A', 'B']
numbers = list(range(6))
arrays = [letters, numbers]
df = pd.DataFrame(np.random.randn(6, 2), index=pd.MultiIndex.from_arrays(arrays), columns=['col1', 'col2'])
print(df)
```
2. 为多重索引设置列名称:
```python
df.columns.names = ['Cols']
print(df)
```
输出:
```
Cols col1 col2
A 0 0.538097 0.208871
1 -0.670527 -0.715467
2 -1.507931 1.601408
B 3 0.295782 -0.261859
4 0.031535 -0.073681
5 0.154152 0.827682
```
3. 多重索引分组求和:
```python
grouped = df.groupby(level=0).sum()
print(grouped)
```
输出:
```
Cols col1 col2
A -1.640361 1.094812
B 0.481470 0.492142
```
4. 行列名称转换:
```python
df = df.stack().reset_index().rename(columns={'level_0': 'Letter', 'level_1': 'Number', 0: 'Value'})
df = df.pivot(index='Number', columns='Letter', values='Value').reset_index().rename_axis(None, axis=1)
print(df)
```
输出:
```
Number A B
0 0 0.538097 NaN
1 1 -0.670527 NaN
2 2 -1.507931 NaN
3 3 NaN 0.295782
4 4 NaN 0.031535
5 5 NaN 0.154152
```
阅读全文