Pandas How to Print a Certain Column

Viewed 7493

I have a dataframe that looks like this:

     security_group    test     test_2   test_3
0          group a       1        Jon    blue
1          group b       2        bob    green

I want to print (not delete from the dataframe, just simply print. I don't want to actually modify the dataframe) 1 of the columns. For example:

      test_3
0       blue
1      green

I tried doing the following:

print(df([test_3]))

This generates the following error message: NameError: name "test_3" is not defined

Any suggestions?

2 Answers

test_3 should be in quotes to denote that it is a string "test_3" otherwise python will think you're attempting to refer to a variable you named test_3 and upon failure to find a variable named test_3 you'll receive the error you're getting. Also your parentheses after df are incorrect. You only need square brackets:

print(df["test_3"])

Use quotes around column name:

print(df['test_3'])

OR:

print(df.test_3)
Related