简体   繁体   English

如何获取Pandas Dataframe的最后一行号?

[英]How to get the last line number of Pandas Dataframe?

I'm trying to get the last row elements from a CSV file.我正在尝试从 CSV 文件中获取最后一行元素。

My code so far -到目前为止我的代码 -

from google.cloud import storage
import pandas as pd

temp = pd.read_csv('gs://my-bucket/my_file.csv',header=None)
print(temp[0][6]) -> Key 0 is obvious as the first element in last row but how to get no. 6 dynamically?

It prints -它打印 -

BO

My sample CSV file -我的样本 CSV 文件 -

John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123
BO,Testing,6

What I want to know that the no.我想知道的是没有。 of records in the real file coming in would be changing every day.真实文件中的记录每天都在变化。 So how would I get the last row no.那么我如何得到最后一行没有。 without reading the entire file and increment a counter?不读取整个文件并增加一个计数器?

You can get the last row of a Dataframe by using the -1 index.您可以使用 -1 索引获取 Dataframe 的最后一行。

In your example temp.iloc[-1] , or temp.iloc[-1].values if you want to get the values from the last dataframe row as an array.在您的示例temp.iloc[-1]temp.iloc[-1].values ,如果您想从最后一个 dataframe 行中获取值作为数组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM