Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何隐藏行索引_Python_Pandas - Fatal编程技术网

Python 如何隐藏行索引

Python 如何隐藏行索引,python,pandas,Python,Pandas,我想将此数据帧写入不带索引值的xlsx文件。我怎么做 writer=pd.ExcelWriter(r"D:\pandas.xlsx") today=datetime.datetime.today() header = pd.MultiIndex.from_product([[today],["name","lastname","age"]]) data=pd.DataFrame(newList, columns=header) data.to_excel(writer) writer.save(

我想将此数据帧写入不带索引值的xlsx文件。我怎么做

writer=pd.ExcelWriter(r"D:\pandas.xlsx")
today=datetime.datetime.today()
header = pd.MultiIndex.from_product([[today],["name","lastname","age"]])
data=pd.DataFrame(newList, columns=header)
data.to_excel(writer)
writer.save()
结果:

  2019-09-16 18:23:20.851291              
                        name  lastname age
0                        John  McBrain  22
1                     Patrick    Heszke 33
2                      Luk         Nans 21

我需要:

  2019-09-16 18:23:20.851291              
                        name  lastname age
                        John  McBrain  22
                     Patrick    Heszke 33
                         Luk      Nans 21

以下是异常的解决方法:

NotImplementedError: Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.
df.columns
是一个多索引时,调用
df.to_excel(writer,index=False)
时会出现这种情况

import numpy as np
import pandas as pd

np.random.seed(2019)

def write_excel(df, path, *args, **kwargs):
    """
    Write df as an excel file to path, roughly similar to `df.to_excel` except that it handles
    `df` with MultiIndex columns and `index=False`.
    """
    writer = pd.ExcelWriter(path)

    header = pd.DataFrame(df.columns.to_list()).T
    header.to_excel(writer, header=False, *args, **kwargs)

    # Avoid the "NotImplementedError: Writing to Excel with MultiIndex columns"
    # exception by temporarily changing the columns to a single-level index
    orig_columns = df.columns
    df.columns = range(len(df.columns))
    df.to_excel(
        writer, startrow=len(header), header=False, *args, **kwargs
    )
    df.columns = orig_columns
    writer.save()

df = pd.DataFrame(np.random.randint(10, size=(5, 4)), columns=list("ABCD"))
df = df.set_index(list("AB")).unstack("B")
write_excel(df, r"/tmp/pandas.xlsx", sheet_name="Sheet1", index=False)    
print(df)
转换数据帧,
df

     C              D          
B    2    5    8    2    5    8
A                              
0  5.0  NaN  NaN  7.0  NaN  NaN
6  NaN  NaN  0.0  NaN  NaN  0.0
7  NaN  NaN  5.0  NaN  NaN  3.0
8  5.0  4.0  NaN  8.0  0.0  NaN
到一个看起来像


在excel的
中有一个
index=False
参数
;这就是你需要的吗?否则,如何打印(df.to_string(index=False))?当我在excel中使用index=False时,会出现错误(使用多索引列写入excel,但没有实现任何索引('index'=False)),这是非常不幸的。需要明确的是,这里的目标是在不使用索引的情况下写入excel文件,还是仅仅希望将此结果打印到某个控制台或文本文件?您还可以执行
df.reset\u index(drop=True)。to\u excel(index=False)
?df.reset\u index(drop=True)。to\u excel(index=False)-相同的错误。我需要把它写给我…非常有用:)+1