首页 > 解决方案 > 如何从excel文件的第一列获取值作为数组?

问题描述

嗨,我想从 excel 文件的第一列中获取值作为数组。

我已经写了这段代码-

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])

我现在得到的输出

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

我想得到什么

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

有人可以帮忙吗?感谢:D

标签: pythonexcelpandas

解决方案


您只需要使用 pandas 的tolist函数:

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())

输出

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

推荐阅读