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