从 pandas 系列数据框中显示/打印一列 [英] Display/Print one column from a DataFrame of Series in Pandas
本文介绍了从 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'])
我只想显示/打印DataFrame中的一列(带或不带标题行):
I want to display/print out just one column from the DataFrame (with or without the header row):
Adam
Bob
Cathy
或:
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?
推荐答案
通过使用 to_string
print(df.Name.to_string(index=False))
Adam
Bob
Cathy
这篇关于从 pandas 系列数据框中显示/打印一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文