【发布时间】:2021-11-09 09:01:38
【问题描述】:
尝试将单个数据框列转换为行。我用以下代码抓取了一个网站:
import requests
from bs4 import BeautifulSoup
import pandas as pd
URL = 'https://www.kemendag.go.id/id'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
table =soup.select('table')[1]
columns = table.find_all('thead')
column_ = []
for column in columns:
cols = column.find_all('th')
cols = [item.text.strip() for item in cols]
column_.append([item for item in cols if item])
rows = table.find_all('tr')
output = []
for row in rows:
x = row.find_all('td')
x = [item.text.strip() for item in x]
output.append([item for item in x if item])
df = pd.DataFrame(output, columns=column_)
这是输出的样子:
Tahun Jan Feb Mar Apr Mei Jun Jul Ags Sep Okt Nov
0 2020 0.39 0.28 0.10 0.08 0.07 0.18 -0.10 -0.05 -0.05 0.07 0.28
1 2021 0.26 0.10 0.08 0.13 0.32 -0.16 0.08 0.00 0.00 0.00 0.00
我希望它看起来像:
Tahun Month Value
2020 Jan 0.39
2020 Feb 0.28
2020 Mar 0.10
2020 Apr 0.08
2020 Mei 0.07
2020 Jun 0.18
2020 Jul -0.10
2020 Ags -0.05
2020 Sep -0.05
2020 Okt 0.07
2020 Nov 0.28
2021 Jan 0.26
2021 Feb 0.10
2021 Mar 0.08
2021 Apr 0.13
2021 Mei 0.32
2021 Jun -0.16
2021 Jul 0.08
2021 Ags 0.00
2021 Sep 0.00
2021 Okt 0.00
2021 Nov 0.00
问题是我试过了
df.melt(id_vars=["Tahun"],
var_name="Month",
value_name="Value")
但它得到了错误TypeError: only integer scalar arrays can be converted to a scalar index,知道吗?谢谢,我也试过了
print(
df.set_index(["Tahun"])
.stack()
.reset_index(name="Value")
.rename(columns={"level_2": "Month"})
.sort_values("Month")
.reset_index(drop=True)
)
得到同样的错误
【问题讨论】:
-
but it got error- 什么是错误?
标签: python pandas dataframe transpose