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
CathyOr:
Sweet
Candy
ChocolateI 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?
33 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
ChocolateOption 2
for v in df['Item']: print(v)
Sweet
Candy
Chocolate