Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas How to Print a Certain Column [duplicate]

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?

like image 621
Chicken Sandwich No Pickles Avatar asked Oct 28 '25 10:10

Chicken Sandwich No Pickles


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"])
like image 161
Cameron Riddell Avatar answered Oct 30 '25 00:10

Cameron Riddell


Use quotes around column name:

print(df['test_3'])

OR:

print(df.test_3)
like image 43
Mayank Porwal Avatar answered Oct 30 '25 01:10

Mayank Porwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!