Python // Pandas: 列不会重命名

huangapple go评论63阅读模式
英文:

Python // Pandas: Column Won't Rename

问题

# 更改“Day”列名可能与“Day”列作为索引而不是列有关吗?
# 这是我的代码和未处理数据示例:

# 导入pandas和numpy库
import pandas as pd
import numpy as np

# 将excel文件加载到DataFrame中
df = pd.read_excel("Marginal CPA data - NOV.xlsx")

# 删除底部行
df = df[:-1]

# 过滤名为“Campaign Type (Search ACQ) - ONC”的列,只保留值为“NonBrand”的行
df = df[df["Campaign Type (Search ACQ) - ONC"] == "NonBrand"]

# 制作一个数据透视表
pivot_table = pd.pivot_table(df, values=["Media Cost", "CAFE Approvals"],
                              index=["Campaign Type (Search ACQ) - ONC", "Product (ACQ Search) - ONC", "Day"],
                              columns=["CDJ"], aggfunc="sum")

df_pivot = pivot_table.fillna(value=0)

# 将列索引重置为单一级别
df_pivot.columns = ["_".join(col) for col in df_pivot.columns]

# 重命名列
df_pivot = df_pivot.rename(columns={"Media Cost_CPA": "CPA Spend", "Media Cost_Non CPA (CDJ)": "CDJ Spend",
                                     "CAFE Approvals_CPA": "CPA Approvals", "CAFE Approvals_Non CPA (CDJ)": "CDJ Approvals",
                                    "Day": "Date"})
英文:

The "Day" column won't rename. Could it have something to do with the "Day" column being an index and not a column? Here's my code and a sample of the unprocessed data:

import pandas as pd
import numpy as np

# Load the excel file into a dataframe
df = pd.read_excel("Marginal CPA data - NOV.xlsx")

# Delete the bottom row
df = df[:-1]

# Filter the column labeled "Campaign Type (Search ACQ) - ONC" to keep only rows with value "NonBrand"
df = df[df["Campaign Type (Search ACQ) - ONC"] == "NonBrand"]

# Make a pivot table
pivot_table = pd.pivot_table(df, values=["Media Cost", "CAFE Approvals"],
                              index=["Campaign Type (Search ACQ) - ONC", "Product (ACQ Search) - ONC", "Day"],
                              columns=["CDJ"], aggfunc="sum")

df_pivot = pivot_table.fillna(value=0)

# Reset the column index to a single level
df_pivot.columns = ["_".join(col) for col in df_pivot.columns]


 # Rename columns
df_pivot = df_pivot.rename(columns={"Media Cost_CPA": "CPA Spend", "Media Cost_Non CPA (CDJ)": "CDJ Spend",
                                     "CAFE Approvals_CPA": "CPA Approvals", "CAFE Approvals_Non CPA (CDJ)": "CDJ Approvals",
                                    "Day": "Date"})
Campaign Type (Search ACQ) - ONC Product (ACQ Search) - ONC CDJ Day Media Cost CAFE Approvals
NonBrand Consumer CPA 11 Jan 2023 29019.77415 94
NonBrand Consumer Non CPA (CDJ) 17 Jan 2023 24640.36448 86
NonBrand Consumer Non CPA (CDJ) 12 Jan 2023 23627.78256 78
NonBrand Student CPA 17 Jan 2023 29863.95447 152
NonBrand Miles CPA 23 Jan 2023 380.94 1
NonBrand Miles CPA 07 Jan 2023 1786.51 5
NonBrand Consumer CPA 19 Jan 2023 26745.81705 64
NonBrand Secured CPA 20 Jan 2023 1551.35 19
NonBrand Consumer Non CPA (CDJ) 02 Feb 2023 41185.11225 66

答案1

得分: 1

cols = {
    "Media Cost_CPA": "CPA花费", 
    "Media Cost_Non CPA (CDJ)": "CDJ花费",
    "CAFE Approvals_CPA": "CPA批准", 
    "CAFE Approvals_Non CPA (CDJ)": "CDJ批准"
}
indx = {
    "Day": "日期"
}
df_pivot = df_pivot.rename(columns=cols).rename_axis(index=indx)
英文:

rename_axis might work better for a MultiIndex:

cols = {
    "Media Cost_CPA": "CPA Spend", 
    "Media Cost_Non CPA (CDJ)": "CDJ Spend",
    "CAFE Approvals_CPA": "CPA Approvals", 
    "CAFE Approvals_Non CPA (CDJ)": "CDJ Approvals"
}
indx = {
    "Day": "Date"
}
df_pivot = df_pivot.rename(columns=cols).rename_axis(index=indx)

huangapple
  • 本文由 发表于 2023年2月16日 02:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定