D
D
demegorych2022-02-27 11:20:39
Python
demegorych, 2022-02-27 11:20:39

How to access dataframe row via element value (pandas)?

Hello,
I have a csv file:

user_id,name,lastname
100288,Petr,Ivanov
100289,Egor,Petrov
...


I read it through
df_csv1 = pd.read_csv(
    r'name.csv',  
    delimiter=',',  
)

Everything reads and outputs well, the question is how can I access, for example, the second line with user_id = 100289, despite the fact that user_id does not start from zero and one and user_id are out of order (are there gaps)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AVKor, 2022-02-27
@demegorych

import pandas as pd

CSV = 'test.csv'
with open(CSV, 'r', encoding='utf-8') as f:
    df = pd.read_csv(f)

row = df[df['user_id'] == 100289]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question