panda用数据帧值作为字符串填充列表

2024-10-03 19:30:22 发布

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

我正在从文件夹中读取csv文件,并将tem过滤到pandas数据帧中,如下所示:

results=[]
for filename in glob.glob(os.path.join('/path/*.csv')):
  with open(filename) as p:
    df = pd.read_csv(p)

    filtered = df[(df['duration'] > low1) & (df['duration'] < high1)]

    artist = filtered['artist'].values
    print artist
    track = filtered['track'].values
    print track

其中low1 = 0high_1 = 0.5

artisttrack将数百个过滤后的项目作为普通字符串打印出来,但是如果我尝试将它们附加到循环中的results中:

^{pr2}$

{I>和填充了

如何用常规的results填充results,以这种方式:

[['artist1', 'track1'], ['artist1', 'track2], ...]]

Tags: csvpath文件夹dfartisttrackfilenameresults
1条回答
网友
1楼 · 发布于 2024-10-03 19:30:22

创建DataFrame的列表,然后通过^{}将它们连接在一起,最后转换为嵌套列表:

results=[]
for filename in glob.glob(os.path.join('/path/*.csv')):
    df = pd.read_csv(filename)
    #filter by conditions and also columns by names with .loc
    filtered = df.loc[(df['duration'] > low1) & (df['duration'] < high1), ['artist','track']]
    #alternative solution 
    filtered = df.loc[df['duration'].between(low1, high1,inclusive=False), ['artist','track']]
    results.append(filtered) 

out = pd.concat(results).values.tolist()

另一个解决方案id追加列表,最后通过列表理解将其展平:

^{pr2}$

相关问题 更多 >