Skip to content
Advertisement

When I read a csv with squeeze is set to True in Pandas it makes dataframe not series

Was able to import a CSV while trying to perform squeeze but it looks like a data frame instead of a series?

This is what I tried

import pandas as pd
import numpy as np

dnd_name = pd.read_csv(r"dnd-dataframe.csv", usecols = ["name"], squeeze = True)
dnd_name.head()

this is what I expected at least I remember learning the output was not a data frame but more like a series?

I was expecting it would come in like a series and less like data frame and look something like this

name    
bam     Bard        Dagger, sling, club          Transmutation, Enchantment
niem    Sorcerer    light crossbow, battleaxe    Necromancy
aem     Paladin     Greataxe                     Abjuration, Conjuration
yaeks   Rogue       club, battleaxe              Conjuration, Evocation, Transmutation
jeeks   Druid       Dagger, Greataxe             Evocation, Transmutation, Necromancy
Name: Type, dtype: object

this is the errors

But instead, I received an error

    import pandas as pd
    import numpy as np
    
    dnd_name = pd.read_csv(r"dnd-dataframe.csv", usecols = ["name"], squeeze = True)
    dnd_name.head()

error received


    usecols_dtype = lib.infer_dtype(usecols, skipna=False)
    
    ValueError: 'usecols' must either be list-like of all strings, all unicode, all integers or a callable.



I also was tried this which worded but also didn’t look as expected

    import pandas as pd
    import numpy as np
        
    dnd_name = pd.read_csv(r"dnd-dataframe.csv", index_col = "name", squeeze = True)
    dnd_name.head()

Note: non-picture data frame below

link to actual dataframe csv csv on github


![image dataframe](https://i.stack.imgur.com/GOEAk.png)

I can import it normally into a data frame and I get the

import pandas as pd
import numpy as np

dnd_df = pd.read_csv(r"dnd-dataframe.csv")
dnd_df.head()


    name    herotype    weapons                      spells
0   bam     Bard        Dagger, sling, club          Transmutation, Enchantment
1   niem    Sorcerer    light crossbow, battleaxe    Necromancy
2   aem     Paladin     Greataxe                     Abjuration, Conjuration
3   yaeks   Rogue       club, battleaxe              Conjuration, Evocation, Transmutation
4   jeeks   Druid       Dagger, Greataxe             Evocation, Transmutation, Necromancy

After fixing import using siamak safari comments

    import pandas as pd
    import numpy as np
        
    dnd_names = pd.read_csv(r"dnd-dataframe.csv", usecols = ["name", "herotype", "weapons", "spells"], squeeze = True)
    dnd_names.head()
    name    herotype    weapons     spells
0   bam     Bard    Dagger, sling, club     Transmutation, Enchantment
1   niem    Sorcerer    light crossbow, battleaxe   Necromancy
2   aem     Paladin     Greataxe    Abjuration, Conjuration
3   yaeks   Rogue   club, battleaxe     Conjuration, Evocation, Transmutation
4   jeeks   Druid   Dagger, Greataxe    Evocation, Transmutation, Necromancy

As pointed out by Gregor what I am trying will not work with multiple columns but if i make new csv with only 2 columns one for index and another it works.

enter image description here

dnd_name1 = pd.read_csv(r"dnd-dataframe-v2.csv", index_col = ["name"], squeeze = True)
dnd_name1.head()

name
bam          Bard
niem     Sorcerer
aem       Paladin
yaeks       Rogue
jeeks       Druid
Name: herotype, dtype: object

Advertisement

Answer

The parameter squeeze is DEPRECATED since pandas version 1.4.0.

Append .squeeze("columns") to a dataframe, and you will get a series.

See document: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement