0

I'm just starting up with python and am struggling to extract a value from my first column, at the end of the dataframe.

so let's say I have a .csv file with 3 columns:

id,name,country
1,bob,USA
2,john,Brazil
3,brian,austria

i'm trying to extract '3' from the ID column (last row ID value)

fileName=open('data.csv')
reader=csv.reader(fileName,delimiter=',')
count=0
for row in reader:
     count=count+1

I'm able to get the rowcount, but am unsure how to get the value from that particular column

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
Koosh
  • 876
  • 13
  • 26

4 Answers4

2

this should do the job:

import csv

fileName=open('123.csv')
reader=csv.reader(fileName,delimiter=',')
count=0
for row in reader:
     if count == 3:
        print(row[0])
     count=count+1

but its better to import pandas and convert your csv file to a dataframe by doing this :

import csv
import pandas as pd

fileName=open('123.csv')
reader=csv.reader(fileName,delimiter=',')

df = pd.DataFrame(reader)
print(df.loc[3][0])

it would be easier to grab whatever element you want.

using loc, you can access any element using the row number and the column number, for example you wanted to grab the element 3 which is on the row 3, column 0, so you just grab it by df.loc[3][0]

if you don't have pandas installed, install it in the command prompt using the command:

pip install pandas
1

I found your question a bit ambiguous, so I'm answering for both cases.

If you need the first column, third row value:

value = None
with open('data.csv') as fileName:
    reader = csv.reader(fileName, delimiter=',')
    for row_number, row in enumerate(reader, 1):
        if row_number == 3:
            value = row[0]

If you need the first column, last row value:

value = None
with open('data.csv') as fileName:
    reader = csv.reader(fileName, delimiter=',')
    for row in reader:
        value = row[0]

In both cases, value has the value you want.

jfaccioni
  • 7,099
  • 1
  • 9
  • 25
1

As mentioned in the comments df['id'].iloc[-1] will return the last id value in the DataFrame which in this case is what you want.

You can also access based on the values in the other rows. For example:

df.id[(df.name == 'brian')] would also give you a value of 3 because brian is the name associated with an id of 3.

You also don't have to loop through the DataFrame rows to get the size, but when you have the DataFrame loaded can simply do count = df.shape[0] which will return the number of rows.

Denver
  • 629
  • 4
  • 6
1

Given that you are starting with Python, and looking at the code provided, I think this Idiomatic Python video will be super helpful. Transforming Code Into Beautiful, Idiomatic Python | Raymond Hettinger

In addition to pandas documentation referenced below, this summary is pretty helpful as well: Select rows in pandas MultiIndex DataFrame.

Pandas indexing documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

jedi
  • 525
  • 4
  • 11