如何删除“列索引”在 Geopandas 数据框中?

发布于 2025-01-20 23:51:12 字数 495 浏览 3 评论 0 原文

我从这里下载了全局二氧化碳排放的.csv:

将其放入Geopandas DataFrame中,并将其与全世界的Shapefile合并。 在排放的.CSV中,列名称都是正确的。但是,在执行 ghg.head()之后,在原始列名称上方有一行,带有'field_1','field_2'...

我想知道如何删除该行不需要的列名称,而下面的行是列名。我已连接 GHG.head90 以下输出。 输出图像

请让我知道我能做什么,谢谢。

I downloaded a .csv of Global CO2 emission from here: https://data.worldbank.org/indicator/EN.ATM.CO2E.KT?locations=EU

Put it into a GeoPandas DataFrame and merged it with a shapefile of the whole world.
In the .csv of emissions, the column names are all correct. However, after doing ghg.head(), there is a row above the original column names, with 'field_1', 'field_2' ...

I was wondering how I would go about removing that row of unwanted column names, and instead have the row below be the column names. I have attached the ghg.head90 output below. Image of output

Please let me know what I can do, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2025-01-27 23:51:12

已下载相同的文件。关键部分是在加载 CSV 时跳过前三行。

import pandas as pd
from pathlib import Path
import geopandas as gpd

# https://api.worldbank.org/v2/en/indicator/EN.ATM.CO2E.KT?downloadformat=csv
f = Path.home().joinpath(
    "Downloads/API_EN/API_EN.ATM.CO2E.KT_DS2_en_csv_v2_3888754.csv"
)
df = pd.read_csv(f, skiprows=3)
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

# join data and geometry
gdf = gpd.GeoDataFrame(df.merge(world, left_on="Country Code", right_on="iso_a3"))

# now generate a choropleth
gdf.loc[
    :, ["Country Name", "Country Code", "Indicator Name", "2018", "geometry"]
].explore(
    column="2018",
    vmin=gdf["2018"].quantile(0.25),
    vmax=gdf["2018"].quantile(0.9),
    height=300,
    width=500,
)

输入图片此处描述

Have downloaded same file. Key part is to skip first three rows when you load CSV.

import pandas as pd
from pathlib import Path
import geopandas as gpd

# https://api.worldbank.org/v2/en/indicator/EN.ATM.CO2E.KT?downloadformat=csv
f = Path.home().joinpath(
    "Downloads/API_EN/API_EN.ATM.CO2E.KT_DS2_en_csv_v2_3888754.csv"
)
df = pd.read_csv(f, skiprows=3)
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))

# join data and geometry
gdf = gpd.GeoDataFrame(df.merge(world, left_on="Country Code", right_on="iso_a3"))

# now generate a choropleth
gdf.loc[
    :, ["Country Name", "Country Code", "Indicator Name", "2018", "geometry"]
].explore(
    column="2018",
    vmin=gdf["2018"].quantile(0.25),
    vmax=gdf["2018"].quantile(0.9),
    height=300,
    width=500,
)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文