pandas 如何使用pandas从excel文件中读取特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30645276/
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 specific rows from excel file using pandas
提问by Dipole
I have an excel file and I need to extract certain data from the rows of a certain sheet. So far I have
我有一个 excel 文件,我需要从某个工作表的行中提取某些数据。到目前为止我有
import pandas as pd
xl_file = pd.ExcelFile((xlfilePath)
dfs = {sheet_name: xl_file.parse(sheet_name) for sheet_name in xl_file.sheet_names}
Now I would like to read the numerical values found in a particular row. The row structure is something like:
现在我想读取在特定行中找到的数值。行结构类似于:
Length (mm) 10.1 - 16.0 - 19.5 - 16.4 - 11.3
where I am attempting to show what is in each cell of a row. The dashes indicate an empty entry in a cell! How can I read in a row like this using the pandas library? I happen to know what row number the above row has but would there be a way for pandas to look through the data frame and find the entry length (mm)instead of having to specify the row number?
我试图显示一行的每个单元格中的内容。破折号表示单元格中的空条目!如何使用 Pandas 库像这样连续阅读?我碰巧知道上面一行的行号是什么,但是有没有办法让Pandas查看数据框并找到条目length (mm)而不必指定行号?
Edit: The actual df.loc['length (mm)'] as suggested by EdChum looks like this:
编辑:EdChum 建议的实际 df.loc['length (mm)'] 如下所示:
0 17.92377
Unnamed: 1 NaN
0.05 18.55764
Unnamed: 3 NaN
0.1 19.17039
Unnamed: 5 NaN
0.15 19.7507
Unnamed: 7 NaN
0.2 20.29776
Unnamed: 9 NaN
0.25 20.80492
Unnamed: 11 NaN
0.3 21.2667
Unnamed: 13 NaN
0.35 21.67687
Unnamed: 15 NaN
0.4 22.02884
Unnamed: 17 NaN
0.45 22.3156
Unnamed: 19 NaN
0.5 22.53051
Unnamed: 21 NaN
0.55 22.66691
Unnamed: 23 NaN
0.6 22.71949
Unnamed: 25 NaN
0.65 22.68477
Unnamed: 27 NaN
0.7 22.56162
Unnamed: 29 NaN
0.75 22.35258
Unnamed: 31 NaN
0.8 22.06432
Unnamed: 33 NaN
0.85 21.7079
Unnamed: 35 NaN
0.9 21.29801
Unnamed: 37 NaN
0.95 20.85419
Unnamed: 39 NaN
1 20.394
Name: length (mm), dtype: object
采纳答案by EdChum
After loading your df you can select a specific row using label indexing loc:
加载 df 后,您可以使用标签索引选择特定行loc:
df.loc['length (mm)']
If you want a np.array from this just do:
如果你想要一个 np.array 就这样做:
df.loc['length (mm)'].values
回答by Pralhad Narsinh Sonar
While reading the file you can always specify the column name in the form of parameter to data frame.
在读取文件时,您始终可以以数据框的参数形式指定列名。
import pandas as pd
fields = ['employee_name']
d_frame = pd.read_csv('data_file.csv', skipinitialspace=True, usecols=fields)
# get the required key or column name
print(d_frame.keys())
# Get data from column name
print(d_frame.employee_name)
I assumed employee_namewould be the column name that you want to fetch the value from.
我假设employee_name将是您要从中获取值的列名。

