Python Pandas - 删除列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44917675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:37:25  来源:igfitidea点击:

Pandas - delete column name

pythonpandas

提问by lil

I want to delete to just column name (x,y,z) use only data

我想删除到仅列名 (x,y,z) 仅使用数据

In [68]: df
Out[68]: 
   x  y  z
0  1  0  1  
1  2  0  0 
2  2  1  1 
3  2  0  1 
4  2  1  0

i want to print result to same as below.

我想打印结果与下面相同。

Out[68]: 

0  1  0  1  
1  2  0  0 
2  2  1  1 
3  2  0  1 
4  2  1  0

is it possible? how i can do this?

是否可以?我怎么能做到这一点?

回答by jezrael

In pandas by default need column names.

在 Pandas 中默认需要列名。

But if really want 'remove'columns what is strongly not recommended, because get duplicated column names is possible assign empty strings:

但是如果真的想要'remove'列什么是强烈不推荐的,因为获取重复的列名可能会分配空字符串:

df.columns = [''] * len(df.columns)


But if need write dfto file without columns and index add parameter header=Falseand index=Falseto to_csvor to_excel.

但是如果需要写入df没有列和索引的文件,请添加参数header=Falseand index=Falseto to_csvor to_excel

df.to_csv('file.csv', header=False, index=False)

df.to_excel('file.xlsx', header=False, index=False)

回答by AChampion

If all you need is to print out without the headers then you can use the to_string()and set header=False, e.g.:

如果您只需要在没有标题的情况下打印出来,那么您可以使用to_string()and set header=False,例如:

>>> print(df.to_string(header=False))
0  1  0  1
1  2  0  0
2  2  1  1
3  2  0  1
4  2  1  0

回答by Sajin Sabu

If you need to remove the header alone, uses '.values'.

如果您需要单独删除标题,请使用“.values”。

df = df[:].values

But the above code will return a numpy array instead of dataframe. Converting the same again into dataframe will add default values to column names (0,1..).

但是上面的代码将返回一个 numpy 数组而不是数据帧。再次将其转换为数据帧将为列名(0,1..)添加默认值。