我有一个现有的数据框,其中包含从 CSV 文件中提取的 Country 、 Weather 、 Temp 、 Date 。 我想将数据框转换为多索引并导出到 excel 文件
df = pd.read_csv('Country.csv')
df
Out[47]:
Country Weather Temp Date
0 Korea Sunny 30 20201201
1 Japan Rainny 26 20201201
2 England Sunny 27 20201201
3 Malaysia Rainny 25 20201201
4 Korea Sunny 30 20201202
5 Japan Rainny 26 20201202
6 England Sunny 27 20201202
7 Malaysia Rainny 25 20201202
8 Korea Rainny 30 20201203
9 Japan Rainny 26 20201203
10 England Rainny 27 20201203
11 Malaysia Rainny 28 20201203
我曾尝试使用 df.set_index(['Country', 'Date'])
但它不起作用
输出版本
答案 0 :(得分:1)
试试这个:
<div id="login">
<img src="./logo.png" class="loginLogo">
<div class="loginForm">
<label for="username">Username</label>
<input type="text" name="username" placeholder="Enter username">
<label for="password">Password</label>
<input type="password" name="password" placeholder="Enter password">
<button class="navButtonOne">Login</button>
</div>
</div>
输出:
df.set_index(['Country', 'Date']).unstack(1).swaplevel(0,1, axis=1).sort_index(axis=1)