首页 > 解决方案 > 从没有标题的数据框中获取列表-pandas python

问题描述

我有很多没有标题的 CSV。它们都只有 1 列,并且有一堆链接。

我正在尝试遍历所有 CSV 并将它们放入单个 python 列表中,以便我可以在列表上执行 for 循环。

从我所见,您似乎只能在用于列to_list()时使用names,但是没有标题名称的 CSV 呢?

代码:

def pandadownload():
    listgather() #this function grabs all the CSVs in a directory and returns a list
    csvlist = listgather.csvval
    for csv in csvlist:
        pandadownload.df = pd.read_csv(csv,index_col=0, header=None)
        print(pandadownload.df)
    return pandadownload.df

结果(它看起来像一个列表,但是当我尝试在其上使用 for 循环时,它找不到任何东西):

Empty DataFrame
Columns: []
Index: [https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/01%2520-%2520Ace%2520Attorney%2520-%2520Court%2520Begins%2520Blue%2520Note%2520Scale.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/02%2520-%2520Phoenix%2520Wright%2520-%2520Objection.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/03%2520-%2520The%2520Steel%2520Samurai.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/04%2520-%2520Justice%2520For%2520All%2520-%2520Court%2520Begins%2520Blue%2520Note%2520Scale.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/05%2520-%2520Miles%2520Edgeworth%2520-%2520Great%2520Revival.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/06%2520-%2520Furio%2520Tigre%2520-%2520Swinging%2520the%2520Tiger.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/07%2520-%2520Trials%2520and%2520Tribulations%2520-%2520Court%2520Begins%2520Blue%2520Note%2520Scale.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/08%2520-%2520Godot%2520-%2520The%2520Fragrance%2520of%2520Dark%2520Coffee.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/09%2520-%2520Rise%2520From%2520the%2520Ashes%2520-%2520End.mp3, https://downloads.khinsider.com/game-soundtracks/album/-t-w-y-a-o-gyakuten-meets-jazz-soul/10%2520-%2520Trucy%2527s%2520Theme%2520-%2520Child%2520of%2520Magic.mp3]

谢谢!!

标签: pythonpandas

解决方案


为了测试,我使用了一个带有以下数据的虚拟 csv:

在此处输入图像描述

我删除了index_col

index_col (int, str, sequence of int / str, or False, default None) 用作 DataFrame 行标签的列,以字符串名称或列索引的形式给出。如果给出了 int / str 序列,则使用 MultiIndex。

注意: index_col=False 可用于强制 pandas 不使用第一列作为索引,例如当您有一个格式错误的文件时,每行末尾都有分隔符。

应用于iloc获取第一列:

import  pandas as pd

def pandadownload():
    # listgather() #this function grabs all the CSVs in a directory and returns a list
    # csvlist = listgather.csvval
    csvlist = ['urls.csv']
    for csv in csvlist:
        pandadownload.df = pd.read_csv(csv, header=None)
        print(pandadownload.df.iloc[:,0].values)
    # return pandadownload.df

pandadownload()

结果:

在此处输入图像描述


推荐阅读