Pandas 如何打印某一列

Lun*_*Box 3 python dataframe pandas

我有一个如下所示的数据框:

     security_group    test     test_2   test_3
0          group a       1        Jon    blue
1          group b       2        bob    green
Run Code Online (Sandbox Code Playgroud)

我想打印(不是从数据框中删除,只是简单地打印。我不想实际修改数据框)1 列。例如:

      test_3
0       blue
1      green
Run Code Online (Sandbox Code Playgroud)

我尝试执行以下操作:

print(df([test_3]))
Run Code Online (Sandbox Code Playgroud)

这会生成以下错误消息:NameError:名称“test_3”未定义

有什么建议么?

Cam*_*ell 8

test_3 应该用引号引起来,表示它是一个字符串,"test_3"否则 python 会认为您正在尝试引用您命名的变量test_3,并且在找不到名为的变量时,test_3您将收到您收到的错误。另外你后面的括号df也不正确。您只需要方括号:

print(df["test_3"])
Run Code Online (Sandbox Code Playgroud)