[英]How to retrieve one column from csv file using python?
im trying to retrieve the age column from one of the csv file , here is what i coded so far.我试图从其中一个 csv 文件中检索年龄列,这是我到目前为止编码的内容。
df = pd.DataFrame.from_csv('train.csv')
result = df[(df.Sex=='female') & (df.Pclass==3)]
print(result.Age)
# finding the average age of all people who survived
print len(result)
sum = len(result)
I printed out the age, because i wanted to see the list of all ages that belong to the colunm of sex that has the value of "female" and the column of class which has the value of "3"我打印了年龄,因为我想查看属于具有“女性”值的性别列和值为“3”的类列的所有年龄的列表
the print result for some reason shows the colunm number and the age next to it, i just want it print the list of ages thats all.由于某种原因,打印结果显示了列号和它旁边的年龄,我只想打印年龄列表。
PassengerId
3 26.0
9 27.0
11 4.0
15 14.0
19 31.0
20 NaN
23 15.0
25 8.0
26 38.0
29 NaN
33 NaN
39 18.0
40 14.0
41 40.0
45 19.0
48 NaN
50 18.0
69 17.0
72 16.0
80 30.0
83 NaN
86 33.0
101 28.0
107 21.0
110 NaN
112 14.5
114 20.0
115 17.0
120 2.0
129 NaN
...
658 32.0
678 18.0
679 43.0
681 NaN
692 4.0
698 NaN
703 18.0
728 NaN
730 25.0
737 48.0
768 30.5
778 5.0
781 13.0
787 18.0
793 NaN
798 31.0
800 30.0
808 18.0
814 6.0
817 23.0
824 27.0
831 15.0
853 9.0
856 18.0
859 24.0
864 NaN
876 15.0
883 22.0
886 39.0
889 NaN
Name: Age, dtype: float64
This is what my program prints, i just want the list of age on the right column only not the passengerID column which is on the left.这是我的程序打印的内容,我只想要右侧列中的年龄列表,而不是左侧的乘客 ID 列。
Thank you谢谢
result.Age
is a pandas Series
object, and so when you print
it, column headers, indices, and data types are shown as well. result.Age
是一个 pandas Series
对象,因此当您print
它时,还会显示列标题、索引和数据类型。 This is a good thing, because it makes the printed representation of the object much more useful.这是一件好事,因为它使对象的打印表示有用得多。
If you want to control exactly how the data is displayed, you will need to do some string formatting.如果要精确控制数据的显示方式,则需要进行一些字符串格式化。 Something like this should do what you're asking for:
这样的事情应该做你所要求的:
print('\n'.join(str(x) for x in result.Age))
If you want access to the raw data underlying that column for some reason (usually you can work with the Series
just as well), without indices or headers, you can get a numpy array with如果您出于某种原因想要访问该列下的原始数据(通常您也可以使用
Series
),没有索引或标题,您可以获得一个 numpy 数组
result.Age.values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.