gpt4 book ai didi

python - 将 csv 文件加载为数据框

转载 作者:行者123 更新时间:2023-11-30 22:37:49 25 4
gpt4 key购买 nike

这是一个菜鸟问题,但是如果不使用 Pandas (pd.read),我如何导入 CSV 文件并将其加载到 DataFrame 对象中,以便我可以调用它(例如 print (loaded_file) )并打印Python (2.7) 中的文件?

最佳答案

unicodecsv 库也可用于读取 .csv 文件。

import unicodecsv
import pandas as pd

def read_csv(data):
""" Returns a list of dicts from .csv file passed in as data"""
with open(data, "rb") as f:
reader = list(unicodecsv.DictReader(f))
return reader

file = read_csv('filename.csv') # call the function and pass in your 'filename'

pd.DataFrame(file) # call pd.DataFrame() function with file as an argument
# to convert it to DataFrame object

关于python - 将 csv 文件加载为数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43757466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com