显示/打印 Pandas 系列 DataFrame 中的一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46122910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display/Print one column from a DataFrame of Series in Pandas
提问by Kane Chew
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):
我只想显示/打印出 DataFrame 中的一列(带或不带标题行):
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?
我可以用一行简单的代码来完成吗?
回答by piRSquared
Not sure what you are really after but if you want to print exactly what you have you can do:
不确定你真正想要的是什么,但如果你想准确地打印你所拥有的,你可以做:
Option 1
选项1
print(df['Item'].to_csv(index=False))
Sweet
Candy
Chocolate
Option 2
选项 2
for v in df['Item']:
print(v)
Sweet
Candy
Chocolate
回答by Tarun Singh
For printing the Name column
用于打印名称列
df['Name']