简体   繁体   English

无法使用 pandas dataframe 重命名表的列名

[英]Can not rename the table's column name using pandas dataframe

I am new in jupyter notebook and python. Recently I'm working in this code but I can't find out the problem.我是 jupyter 笔记本和 python 的新手。最近我在这段代码中工作,但我找不到问题所在。 I want to rename "Tesla Quarterly Revenue(Millions of US $)" and "Tesla Quarterly Revenue(Millions of US $).1" into "Data" and "Revenue" but it not changed.我想将"Tesla Quarterly Revenue(Millions of US $)" and "Tesla Quarterly Revenue(Millions of US $).1"重命名为"Data" and "Revenue" ,但它没有改变。 Here is my code:这是我的代码:

!pip install pandas
!pip install requests
!pip install bs4
!pip install -U yfinance pandas 
!pip install plotly
!pip install html5lib
!pip install lxml
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
url = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0220ENSkillsNetwork23455606-2022-01-01"
html_data  = requests.get(url).text
soup = BeautifulSoup(html_data, 'html5lib')
tesla_revenue = pd.read_html(url, match = "Tesla Quarterly Revenue")[0]
tesla_revenue = tesla_revenue.rename(columns={"Tesla Quarterly Revenue(Millions of US $)":"Date","Tesla Quarterly Revenue(Millions of US $).1":"Revenue"})
tesla_revenue.head()

Here is the Output:这是 Output:

在此处输入图像描述

Could not reproduce the issue, it works as expected.无法重现问题,它按预期工作。 May print your originally .columns and compare the values to your dict - Not sure if the source is interpreted differnt by module versions:可以打印您最初的.columns并将值与您的dict进行比较 - 不确定源代码是否因模块版本而不同:

print(tesla_revenue.columns)

Just in case an alternative:以防万一:

tesla_revenue.columns = ['Date','Revenue']

Example例子

import pandas as pd
url = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0220ENSkillsNetwork23455606-2022-01-01"
tesla_revenue = pd.read_html(url, match = "Tesla Quarterly Revenue")[0]
#tesla_revenue = tesla_revenue.rename(columns={"Tesla Quarterly Revenue(Millions of US $)":"Date","Tesla Quarterly Revenue(Millions of US $).1":"Revenue"})
tesla_revenue.columns = ['Date','Revenue']
tesla_revenue.head()

Output Output

Date日期 Revenue收入
0 0 2022-09-30 2022-09-30 $21,454 21,454 美元
1 1个 2022-06-30 2022-06-30 $16,934 $16,934
2 2个 2022-03-31 2022-03-31 $18,756 18,756 美元
3 3个 2021-12-31 2021-12-31 $17,719 $17,719
4 4个 2021-09-30 2021-09-30 $13,757 13,757 美元

I could reproduce the error.我可以重现错误。 You have a mistake in your column names.您的列名有误。 Instead of "Tesla Quarterly Revenue(Millions of US $)" it is "Tesla Quarterly Revenue (Millions of US $)" with a space between Revenue and the value in brackets.不是“特斯拉季度收入(百万美元)”,而是“特斯拉季度收入(百万美元)”,在收入和括号中的值之间有一个空格。 The same applies to the second column header.第二列header同理。

To avoid this you could also save the column names into a variable like this:为避免这种情况,您还可以将列名保存到这样的变量中:

soup = BeautifulSoup(html_data, 'html5lib')
tesla_revenue = pd.read_html(url, match="Tesla Quarterly Revenue")[0]
col1_name = tesla_revenue.columns[0]
col2_name = tesla_revenue.columns[1]
tesla_revenue = tesla_revenue.rename(columns={col1_name:"Date",col2_name:"Revenue"})
tesla_revenue.head()

This makes the code also a bit more readable:)这也使代码更具可读性:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM