简体   繁体   English

如何使用 pandas 从 csv 中的绝对路径导入数据?

[英]How to import data from absolute path in a csv using pandas?

I have a CSV containing n records and it is filled with absolute paths to the images.我有一个包含 n 条记录的 CSV 并且它充满了图像的绝对路径。 I'd like to import those images into a numpy matrix.我想将这些图像导入 numpy 矩阵。

csv中的数据

import pandas as pd
from PIL import Image
import numpy as np

def load_image( infilename ) :
  img = Image.open( infilename )
  img.load()
  data = np.asarray( img, dtype="int32" )
  return data

df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')

for i in range(len(df)) : 
  print(load_image(df.iloc[i, 0]))

You can store the returned values in list if you want else directly use.如果您想直接使用,可以将返回的值存储在列表中。

You can use the pandas read_csv function.您可以使用 pandas read_csv function。

import pandas as pd

df = pd.read_csv (r'Path where the CSV file is stored\File name.csv')
print (df)

Source: https://datatofish.com/import-csv-file-python-using-pandas/资料来源: https://datatofish.com/import-csv-file-python-using-pandas/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM