在Pandas中保留csv的评论行吗?

2024-10-05 14:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我刚刚开始研究熊猫的世界,我发现的第一个奇怪的csv文件是一个在开头有两行注释(具有不同的列宽)的文件。在

sometext, sometext2
moretext, moretext1, moretext2
*header*
actual data ---
---------------

我知道如何用skiprowsheader=跳过这些行,但是,在使用read_csv时,如何保留这些注释呢?有时注释作为文件元数据是必需的,我不想把它们丢掉。在

有什么想法吗,伙计们?我会非常感谢你的回答。在


Tags: 文件csv数据readdata世界headeractual
2条回答

您可以先读取元数据,然后使用read_csv

with open('f.csv') as file:
    #read first 2 rows to metadata
    header = [file.readline() for x in range(2)]
    meta = [value.strip().split(',') for value in header]
    print (meta)
    [['sometext', ' sometext2'], ['moretext', ' moretext1', ' moretext2']]

    df = pd.read_csv(file)
    print (df)

          *header*
    0  actual data

Pandas是用来读取结构化数据的。在

对于非结构化数据,只需使用内置的^{}

with open('file.csv') as f:
    reader = csv.reader(f)
    row1 = next(reader)  # gets the first line
    row2 = next(reader)  # gets the second line

可以将字符串附加到数据帧,如下所示:

^{pr2}$

But note

Note, however, that while you can attach attributes to a DataFrame, operations performed on the DataFrame (such as groupby, pivot, join or loc to name just a few) may return a new DataFrame without the metadata attached. Pandas does not yet have a robust method of propagating metadata attached to DataFrames.

相关问题 更多 >