简体   繁体   English

如何使用 python Pandas 访问 csv 文件的倒数第二行?

[英]How to access the second to last row of a csv file using python Pandas?

Want to know if I can access the second to last row of this csv file?想知道我是否可以访问此 csv 文件的倒数第二行吗? Am able to access the very last using:我能够使用以下方法访问最后一个:

pd.DataFrame(file1.iloc[-1:,:].values)

But want to know how I can access the one right before the last?但是想知道我如何才能在最后一个之前访问那个?

Here is the code I have so far:这是我到目前为止的代码:

import pandas as pd
import csv


url1 = r"https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/country_data/Austria.csv"

file1 = pd.read_csv(url1)

df1 = pd.DataFrame(file1.iloc[:,:].values)

df1 = pd.DataFrame(file1.iloc[-1:,:].values)


Austria_date = df1.iloc[:,1]

Austria_cum = df1.iloc[:, 4].map('{:,}'.format)


if ( Austria_cum.iloc[0] == 'nan' ):

Essentially I am checking if the the row at that specific col is 'nan', which is True, and after which I want to get the data from the row right before the last.本质上,我正在检查该特定列处的行是否为“nan”,这是真的,然后我想从最后一行之前的行中获取数据。 Please, how would this be done?请问,这个怎么做?

Thank you谢谢

As simple as that:就如此容易:

df1.iloc[-2,:]

To access the data frame at any index you can use the following要访问任何索引处的数据框,您可以使用以下命令

df.iloc[i]

For your ask you need to set the index as -2, which would look like this:根据您的要求,您需要将索引设置为 -2,如下所示:

df.iloc[-2]

Just use negative indices like in your example, but pull out the second to last with -2 instead of the last:只需使用您的示例中的负索引,但使用-2而不是最后一个拉出倒数第二个:

df.iloc[-2,:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 Pandas 的情况下在 Python 中逐行编辑 CSV 文件 - How to edit a CSV file row by row in Python without using Pandas 如何使用python获取CSV文件最后一行的第一个值 - How to get the first value of last row of a CSV file using python 使用 Pandas、python 迭代行 csv 文件 - Iterate row csv file using pandas, python 使用 python 从 csv 文件中获取最后一列的最后一行 - Get last row in last column from a csv file using python 如何使用 iloc [] select pandas dataframe 的倒数第二行 []? - How to select second last row of a pandas dataframe using iloc[]? 给定使用 python 的最后一列的值,我将如何获得 csv 文件中第一列或第二列的值 - How would I get the value of first or second column in csv file given the value of last column using python 删除csv文件的第二行并使用Python修改列 - Removing second row of csv file and modify the columns using Python 如何使用 Pandas 操作 a.csv 文件中的数据并访问特定的行和列? - How to manipulate data in a .csv file using Pandas and access specific row and column? 如何使用python(pandas)更新csv文件中所有行的最后一列值 - How to update the last column value in all the rows in csv file using python(pandas) 在追加模式下使用to_csv时,python pandas新行附加到csv中的最后一行 - python pandas new row attached to last one in csv when using to_csv in append mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM