python - 数据框将标题行转换为熊猫行
问题描述
有一个带有值的df
东风:
165 156 1 test greater 56gsa
-------------------------------------
spin 201 2 normal lesser 12asgs
pine 202 3 fast greater 5sasgs
所需输出:
0 1 2 3 4 5
-------------------------------------
165 156 1 test greater 56gsa
spin 201 2 normal lesser 12asgs
pine 202 3 fast greater 5sasgs
解决方案
如果 DataFrame 是从文件创建的,那么header=None
参数就是你的朋友:
df = pd.read_csv(file, header=None)
如果不是,则将列转换为一行DataFrame
并转换DataFrame.append
为原始数据:
df = df.columns.to_frame().T.append(df, ignore_index=True)
df.columns = range(len(df.columns))
print (df)
0 1 2 3 4 5
0 165 156 1 test greater 56gsa
1 spin 201 2 normal lesser 12asgs
2 pine 202 3 fast greater 5sasgs
推荐阅读
- php - 如何处理带有 www 和不带 www 的域的 Xing-login 插件?
- unity3d - 如何在unity3d中每隔...秒显示一个广告?
- c# - 连接到我的 controller.cs 文件
- c++ - 使用虚拟内存系统调用分配原子数组是否安全?
- python - 使用 networkx 进行 2D 环面互连
- vb.net - 在同步锁中执行批处理文件以等待它的内部进程在 vb.net 中完成
- wordpress - SiteOrigin的页面构建器
- reactjs - React+Typescript 中的 HOC 示例不起作用
- android - 令人耳目一新的片段 - Xamarin
- php - 根据上次在另一个表中使用的结果对表结果进行排序