环境信息
- ModelArts
- Notebook - Multi-Engine 2.0 (python3)
- JupyterLab - Notebook - Conda-python3
- pandas 0.22.0
- JupyterLab - Notebook - Conda-python3
- Notebook - Multi-Engine 2.0 (python3)
Pandas DataFrame reset_index 合并数据后,重新设置索引
import pandas as pd
import numpy as np
df_top = pd.DataFrame(np.ones((3,2)))
df_bottom = pd.DataFrame(np.zeros((3,2)))
df_top
df_bottom
# 合并完成之后,会发现index乱了
new_df = pd.concat([df_top,df_bottom])
new_df
# drop=True 丢弃原来的索引列
new_df.reset_index(drop=True)
# drop=False 保留原来的索引列
new_df.reset_index(drop=False)
# 在使用中,会连续着写
# 连接矩阵,重置index,保留原来的索引列
# 注意:这里没有写 inplace
new_df = pd.concat([df_top,df_bottom]).reset_index(drop=False)
new_df
help
help(new_df.reset_index)
Help on method reset_index in module pandas.core.frame:
reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='') method of pandas.core.frame.DataFrame instance
For DataFrame with multi-level index, return new DataFrame with
labeling information in the columns under the index names, defaulting
to 'level_0', 'level_1', etc. if any are None. For a standard index,
the index name will be used (if set), otherwise a default 'index' or
'level_0' (if 'index' is already taken) will be used.
......
备注
- 欢迎各位同学一起来交流学习心得^_^
- 在线课程、沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。