21

I created the following Series and DataFrame:

import pandas as pd

Series_1 = pd.Series({'Name': 'Adam','Item': 'Sweet','Cost': 1})
Series_2 = pd.Series({'Name': 'Bob','Item': 'Candy','Cost': 2})
Series_3 = pd.Series({'Name': 'Cathy','Item': 'Chocolate','Cost': 3})`
df = pd.DataFrame([Series_1,Series_2,Series_3], index=['Store 1', 'Store 2', 'Store 3'])

I want to display/print out just one column from the DataFrame (with or without the header row):

Either

Adam 
Bob 
Cathy

Or:

Sweet
Candy
Chocolate

I have tried the following code which did not work:

print(df['Item'])
print(df.loc['Store 1'])
print(df.loc['Store 1','Item'])
print(df.loc['Store 1','Name'])
print(df.loc[:,'Item'])
print(df.iloc[0])

Can I do it in one simple line of code?

3
  • @JohnGalt Hi, I am starting out to learn pandas and trying to grasp the usage of different functions and methods. Commented Sep 8, 2017 at 18:59
  • Are you using IPython and started with 10 Minutes to pandas? Commented Sep 8, 2017 at 19:00
  • @JohnGalt I am running a local session of Jupyter Notebook and following a course on Coursera Commented Sep 8, 2017 at 19:02

3 Answers 3

28

By using to_string

print(df.Name.to_string(index=False))


 Adam
  Bob
Cathy
Sign up to request clarification or add additional context in comments.

1 Comment

to_string makes more sense. Pick this answer please
19

For printing the Name column

df['Name']

Comments

11

Not sure what you are really after but if you want to print exactly what you have you can do:

Option 1

print(df['Item'].to_csv(index=False))

Sweet
Candy
Chocolate

Option 2

for v in df['Item']:
    print(v)

Sweet
Candy
Chocolate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.