python - 数据框将标题行转换为行 Pandas

标签 python pandas dataframe

有一个带值的 df

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参数是你的 friend :

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

关于python - 数据框将标题行转换为行 Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59783735/

相关文章:

python - 我如何在python中按值分配

python - 我们可以限制任何机器学习算法中响应变量的值吗?

python - 确定特定的有序向量是否在列表/数组中

python - 是否有比 .apply() 更慢或更受控制的替代方案?

python - 我应该如何将充满小数的数据框转换为 float ?

python - 导入错误: No module named 'xgboost'

python - 使用 matplotlib 条形图设置列的顺序

python - Python中浮点格式转换为年月格式

python - 按列将 csv 拆分为多个 csv 文件

python - 如何在 Pandas 中选择 'last business day of the month'?