pandas 如何在python中使用pandas读取csv文件的所有行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40801415/
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
How to read all rows of a csv file using pandas in python?
提问by Spider Man
I am using the pandasmodule for reading the data from a .csvfile.
我正在使用该pandas模块从.csv文件中读取数据。
I can write out the following code to extract the data belonging to an individual column as follows:
我可以写出以下代码来提取属于单个列的数据,如下所示:
import pandas as pd
df = pd.read_csv('somefile.tsv', sep='\t', header=0)
some_column = df.column_name
print some_column # Gives the values of all entries in the column
However, the file that I am trying to read now has more than 5000 columns and writing out the statement
但是,我现在尝试读取的文件有 5000 多列并写出语句
some_column = df.column_name
is now not feasible. How can I get all the column values so that I can access them using indexing?
现在不可行。如何获取所有列值以便我可以使用索引访问它们?
e.g to extract the value present at the 100th row and the 50th column, I should be able to write something like this:
例如,要提取第 100 行和第 50 列的值,我应该可以这样写:
df([100][50])
回答by jezrael
Use DataFrame.ilocor DataFrame.iat, but python counts from 0, so need 99and 49for select 100.row and 50.column:
使用DataFrame.ilocor DataFrame.iat,但 python 从 开始计数0,因此需要99和49选择100.行和50.列:
df = df.iloc[99,49]
Sample - select 3.row and 4.column:
示例 - 选择3.行和4.列:
df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9],
'D':[1,3,10],
'E':[5,3,6],
'F':[7,4,3]})
print (df)
A B C D E F
0 1 4 7 1 5 7
1 2 5 8 3 3 4
2 3 6 9 10 6 3
print (df.iloc[2,3])
10
print (df.iat[2,3])
10
Combination for selecting by column name and position of row is possible by Series.ilocor Series.iat:
可以通过Series.iloc或组合按列名和行位置进行选择Series.iat:
print (df['D'].iloc[2])
10
print (df['D'].iat[2])
10
回答by RichSmith
Pandas has indexing for dataframes, so you can use
Pandas 有数据帧的索引,所以你可以使用
df.iloc[[index]]["column header"]
the index is in a list as you can pass multiple indexes at one in this way.
索引位于列表中,因为您可以通过这种方式同时传递多个索引。

