简体   繁体   English

如何使用python从csv文件中检索一列?

[英]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.

相关问题 使用Python将列从一个.csv添加到另一个.csv文件 - Add column from one .csv to another .csv file using Python 使用 Python 将许多实时股票报价检索到一个 CSV 文件中 - Retrieve many live stock quotes to one CSV file using Python 如何使用 python 将三个 csv 文件数据写入一个具有一个日期列和三个数据列的 csv 文件 - how to write three csv file data into one csv file with one date column and three data column using python 如何使用 Python 从 CSV 文件的列中删除英文单词 - How to remove English Words from a column in a CSV file using Python 如何在sikuli中使用python从csv文件中读取单列 - how to read single column from csv file using python in sikuli Python 3,将特定列从一个csv文件复制到另一个csv - Python 3, Copy specific column from one csv file to another csv 使用 Python 3.8 将一个 CSV 文件中的一列(向量)与另一个 CSV 文件中的两列(向量和数组)进行比较 - Compare one column (vector) from one CSV file with two columns (vector and array) from another CSV file using Python 3.8 如何使用 python 在 csv 文件中逐行读取一列数据 - How to read one column data as one by one row in csv file using python 如何从 csv 文件中检索特定的行和列 - How to retrieve a particular row and column from a csv file 在CSV文件的一列上进行计算,然后使用python将其添加到文件中 - Make calculation on one column in CSV file and add it into the file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM