Adam Szymański
Adam Szymański

Reputation: 365

How to get values from first column of excel file as array?

Hi I want to get values from first column of excel file as array.

Already I wrote this code-

import os
import pandas as pd

for file in os.listdir("./python_files"):
    if file.endswith(".xlsx"):
        df = pd.read_excel(os.path.join("./python_files", file))   
        print(df.iloc[:,1])

What i got in output now

0       172081
1       163314
2       173547
3       284221
4       283170
         ...
3582    163304
3583    160560
3584    166961
3585    161098
3586    162499
Name: Organization CRD#, Length: 3587, dtype: int64

What I whish to get

172081
163314
173547
284221
283170
  ...
163304
160560
166961
161098
162499

Can somebody help? Thanks :D

Upvotes: 0

Views: 1860

Answers (2)

Antoine Delia
Antoine Delia

Reputation: 1845

You will just need to use the tolist function from pandas:

import os
import pandas as pd

for file in os.listdir("./python_files"):
    if file.endswith(".xlsx"):
        df = pd.read_excel(os.path.join("./python_files", file)) 
        print(df.iloc[:,1].tolist())

Output

172081
163314
173547
284221
283170
  ...
163304
160560
166961
161098
162499

Upvotes: 3

Rafael Valero
Rafael Valero

Reputation: 2816

As an array:

df.iloc[:,1].values

As a list:

df.iloc[:,1].values.tolist()

Upvotes: 1

Related Questions