Display/Print one column from a DataFrame of Series in Pandas

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

3 Answers

By using to_string

print(df.Name.to_string(index=False)) Adam Bob
Cathy
1

For printing the Name column

df['Name']

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like