0

I would like to get the content of specific row without header column , I'm going to use df.iloc[row number] , but it didn't give me an expected result ? my code as below:

import pandas as pd
df = pd.DataFrame({
    'first_name': ['John', 'Jane', 'Marry', 'Victoria', 'Gabriel', 'Layla'],
    'last_name': ['Smith', 'Doe', 'Jackson', 'Smith', 'Brown', 'Martinez'],
    'age': [34, 29, 37, 52, 26, 32]},
    )
df.head()
df_temp = df.loc[2] 

The result i get is:

first_name      Marry
last_name     Jackson
age                37
Name: 2, dtype: object

I expected it could give me a list , sth like below: ['Marry', 'Jackson','37'] Any idea to do this, could you please advise for my case?

1 Answer 1

1

Well there are many functions in pandas that could help you do this. to_String() or values are a few among them. So if you do something like

import pandas as pd
df = pd.DataFrame({
    'first_name': ['John', 'Jane', 'Marry', 'Victoria', 'Gabriel', 'Layla'],
    'last_name': ['Smith', 'Doe', 'Jackson', 'Smith', 'Brown', 'Martinez'],
    'age': [34, 29, 37, 52, 26, 32]},
    )
df.head()
df_temp = df.loc[2].to_String()
print(df_temp)

you will get an output like this for your given code:

first_name      Marry
last_name     Jackson
age                37

however in your case because you want a list you can just call values and get it as you want. Here's your updated code below:

import pandas as pd
df = pd.DataFrame({
    'first_name': ['John', 'Jane', 'Marry', 'Victoria', 'Gabriel', 'Layla'],
    'last_name': ['Smith', 'Doe', 'Jackson', 'Smith', 'Brown', 'Martinez'],
    'age': [34, 29, 37, 52, 26, 32]},
    )
df.head()
df_temp = df.loc[2].values
print(df_temp)

which will give you the output you probably want as

['Marry' 'Jackson' 37]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.