从dataframe中获取列表,无头

2024-05-13 06:20:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有很多没有标题的CSV。他们都只有一列,他们有一堆链接

我试图迭代所有CSV,并将它们放入一个python列表中,以便在列表上执行for循环

从我所看到的情况来看,当对列使用names时,似乎只能使用to_list(),但是对于没有标题名的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]

谢谢


Tags: csvhttpscomgame列表albumdownloadsmp3
1条回答
网友
1楼 · 发布于 2024-05-13 06:20:43

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

enter image description here

我删除了index_col

index_col (int, str, sequence of int / str, or False, default None) Column(s) to use as the row labels of the DataFrame, either given as string name or column index. If a sequence of int / str is given, a MultiIndex is used.

Note: index_col=False can be used to force pandas to not use the first column as the index, e.g. when you have a malformed file with delimiters at the end of each line.

应用iloc获得第一个coulmn:

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

结果:

enter image description here

相关问题 更多 >