[Python] Skip rows with pandas.read_csv(..., comment="#") but allow hash in the data?

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    Is there any way in pandas to ignore #-commented lines in their entirety, but leave the # symbol alone in the CSV body?

    import pandas as pd
    from io import StringIO

    csv_content = """\
    # title: Sample CSV
    # description: This dataset
    id,name,favourite_hashtag
    1,John,#python
    2,Jane,#rstats
    """

    data = pd.read_csv(StringIO(csv_content),comment='#')
    print(data)


    Right now we get:

    id name favourite_hashtag
    0 1 John NaN
    1 2 Jane NaN


    If no, is there any hope of making a feature request to that end?

    Continue reading...

Compartilhe esta Página