python - 如何将垂直列表转换为panda dataframe?

标签 python pandas

我有一个来自网络爬虫的列表,它在垂直列表中创建了一个日志文件。

示例:

    21-Oct-19 14:46:14 - Retrieving data from https://www.finn.no/bap/forsale/search.html?category=0.93&page=1&product_category=2.93.3904.69&sub_category=1.93.3904
0                          21-Oct-19 14:46:14 - Found:                                                                                                             
1    Title: Nesten ubrukt Canon 17-40 mm vidvinkell...                                                                                                             
2                                      Price: 4�900 kr                                                                                                             
3    Link: https://www.finn.no/bap/forsale/ad.html?...                                                                                                             
4                          21-Oct-19 14:46:14 - Found:                                                                                                             
5    Title: Nesten ubrukt Canon 17-40 mm vidvinkell...                                                                                                             
6                                      Price: 4�900 kr                                                                                                             
7    Link: https://www.finn.no/bap/forsale/ad.html?...                                                                                                             
8                          21-Oct-19 14:46:14 - Found:                                                                                                             
9    Title: Nesten ubrukt Canon 17-40 mm vidvinkell...                                                                                                             
10                                     Price: 4�900 kr                                                                                                             
11   Link: https://www.finn.no/bap/forsale/ad.html?...                                                                                                             
12                         21-Oct-19 14:46:14 - Found:                                                                                                             
13   Title: Nesten ubrukt Canon 17-40 mm vidvinkell...      

我可以将其转换为 Pandas 的可读数据帧吗?

示例:

title           price      link
canon 100mm     6900kr     https
canon 50mm      100r       https
canon 17mm      63530kr    https

我的代码现在看起来像这样:

import pandas as pd

data = pd.read_csv('finn.no-2019-10-21-.log', sep ="Line", engine='python')
df = pd.DataFrame(data)
title = 1,5,9,13,17,21
price = 2,6,10,14,18,22
link = 3,7,11,15,19,23

print(df)

我可以对原始行中的数字进行任何操作以转换为更传统的数据帧吗?

最佳答案

这应该适合你:

with open('finn.no-2019-10-21-.log') as f:
    lines = f.readlines()
    clean = [line.strip() for line in lines]

    title = [j.split('Title: ')[1] for j in clean if j.startswith('Title: ')]
    price = [k.split('Price: ')[1] for k in clean if k.startswith('Price: ')]
    link = [l.split('Link: ')[1] for l in clean if l.startswith('Link: ')]

    df = pd.DataFrame(data=[title, price, link], columns=['Title', 'Price', 'Link'])

关于python - 如何将垂直列表转换为panda dataframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58499424/

相关文章:

python - 这个 matplotlib.finance.candlestick2_ochl 代码会发生什么?

python - 检查某个值在时间范围内是否超过阈值的最佳方法

Python:写入 csv 文件

python - 如何使用 opencv、python、numpy 和必要的库修复下面损坏的图像

python - tensorflow:如何使用不同的条件语句设置张量的形状?

python - 在python中用多个匹配项划分字符串

python - '无法计算 Pack,因为输入 #1(从零开始)预计是浮点张量,但实际上是 int32 张量 [Op :Pack] name: packed'. tf.squeeze 错误

python - Pandas 系列拆分 n 次

python - 将Excel公式转换为python

python - 如何迭代一行并相互比较?