sopana
sopana

Reputation: 375

Read the last line from the CSV file through Pandas

I am trying to read the last line from a CSV file stored in GCS.

My Code -

import pandas as pd
import gcsfs 
fs = gcsfs.GCSFileSystem(project='my-project') 
with fs.open('my-bucket/my_file.csv') as f:  
 file = pd.read_csv(f)  
 print(file.tail(1))

Output:

John       Doe 120 jefferson st. Riverside   NJ   08075
5   business-name Internal                 6       NaN  NaN     NaN

Public Sample CSV file -

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
business-name,Internal,6

I just want to get the last line - business-name,Internal,6 but that's not I'm getting. I'm not sure why tail(1) is not working.

Can anyone please help me?

Upvotes: 1

Views: 963

Answers (2)

harshadbhatia
harshadbhatia

Reputation: 234

By looks of it the code is working correctly. By default it is printing header column. If you want to disable header printing use the follwing.

file.tail(1).to_string(header=False))

Upvotes: 1

mustafasencer
mustafasencer

Reputation: 753

The below pandas code should solve your issue. You can use the pandas read_scv function to get the csv data instead of reading the file.

import pandas as pd
df = pd.read_csv('my-bucket/my_file.csv')
df.tail(1)

Upvotes: 1

Related Questions