当我在Pandas中读取带有squeeze设置为True的csv时,它使 Dataframe 不是系列

3lxsmp7m  于 2022-11-20  发布在  其他
关注(0)|答案(1)|浏览(221)

在尝试执行挤压时能够导入CSV,但它看起来像数据框而不是系列?
这是我试过的

import pandas as pd
import numpy as np

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

这是我所期望的,至少我记得学习的输出不是一个 Dataframe ,而更像是一个系列?
我期待它会像一个系列,而不是像数据框架,看起来像这样

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

这是错误
但是,我收到了一个错误

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

收到错误

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.

我也试过这个,但看起来也不像预期的那样

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()

**注:**下面为非图片数据框

链接到实际 Dataframe csv csv on github

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

我可以正常地将其导入到数据框中,

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

使用siamak safari注解修复导入后

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

正如Gregor所指出的,我正在尝试的方法不会与多列一起工作,但如果我只制作2列的新csv,一列用于索引,另一列用于索引,它就能工作。

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
xuo3flqw

xuo3flqw1#

参数squeeze自panda版本1.4.0起已被弃用
.squeeze("columns")附加到 Dataframe ,您将得到一个系列。
参见文档:https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

相关问题