gpt4 book ai didi

python - Pandas - 使用数据框值作为字符串填充列表

转载 作者:行者123 更新时间:2023-11-28 18:08:39 25 4
gpt4 key购买 nike

我正在从文件夹中读取 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 中:

artist = filtered['artist'].values
track = filtered['track'].values
results.append([track,artist])

我发现我正在追加对象和类型,results 最终填充了一部分筛选项。我不明白会发生什么。

如何以这种方式将所有项目作为常规 strings 填充 results:

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

最佳答案

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

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

另一个解决方案是附加列表,最后通过列表推导将它们展平:

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
mask = df['duration'].between(low1, high1,inclusive=False)
filtered = df.loc[mask, ['artist','track']].values.tolist()
results.append(filtered)

out = [y for x in results for y in x]

关于python - Pandas - 使用数据框值作为字符串填充列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52051521/

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