使用Pandas合并多份电子表格时,我想在同一张表中放入多列,但似乎每个工作表只取到了一列
我之前在这里也提过一个类似的问题,但我觉得解释得不够清楚,所以这次我打算在这里尽量把要点说清楚。我有多张范围从2019年到2023年的电子表格,里面包含员工信息和公司信息。我的代码会把这些表格中的数据进行组合和清洗,生成一个新的数据表。以一个具体例子来说,我有一个suta列(表示State Unemployment Tax Act),它将出现在不同工作表中的5 列合并在一起。
# Combine idast, idsui, and idwd into one column for suta (state unemployment tax)
suta_cols = ["idast", "idsui", "idwd", "id_unemployment_company", "id_workforce_dev_fund"]
existing_cols = [
c for c in suta_cols
if c in combined_2024_2025.columns
]
combined_2024_2025["suta"] = (
combined_2024_2025[existing_cols]
.fillna(0)
.sum(axis=1)
)
就我当前查看的这些工作表而言,唯一被合并的列是:
"id_unemployment_company", "id_workforce_dev_fund"
这两个列在大多数使用到的表格中都存在,且都被合并到suta列中。
随后我尝试将相同的逻辑应用到一个名为“garnishment”的列(garn1):
# Combine garnishments
garn_cols = ["child_support_garnishment", "garnishment_child_support", "wage_garnishment_other", "garnishment_state_tax_levy"]
other_existing_cols = [
c for c in garn_cols
if c in combined_2024_2025.columns
]
combined_2024_2025["garn1"] = (
combined_2024_2025[other_existing_cols]
.fillna(0)
.sum(axis=1)
)
然而,这会导致输出全为0。所以我想知道自己遗漏了什么,以及这段逻辑是否应用正确。
解决方案
在这个过程中有若干点需要检查,以帮助理解并修正可能发生的问题。我创建了一个示例数据集来帮助演示这个过程。需要检查的关键项包括:列名、列的数据类型、缺失值。
示例df
用于演示的样本数据,请注意列名存在不匹配且包含缺失数据。
import pandas as pd
suta_cols = [
"idast",
"idsui",
"idwd",
"id_unemployment_company",
"id_workforce_dev_fund"
]
data = {
"id_ast": [100, 200, None, 150, 175],
"idsui": [50, None, 75, 80, 60],
"id_wd": [25, 30, 45, None, 35],
"id_unemployment_company": [10, 20, 15, 10, None],
"id_workforce_dev_fund": [5, None, 10, 8, 12]
}
df = pd.DataFrame(data)
Check column matching
先看看哪些列在实际中是匹配的。
在这里要特别注意大小写和下划线的使用,idast 与 id_ast 或 IDast 不同。
suta_cols = ["idast", "idsui", "idwd", "id_unemployment_company", "id_workforce_dev_fund"]
existing_cols = [
c for c in suta_cols
if c in df.columns
]
print(f"Number of columns not matched: {len(suta_cols) - len(existing_cols)}")
print(suta_cols)
print(existing_cols)
输出
Number of columns not matched: 2
['idast', 'idsui', 'idwd', 'id_unemployment_company', 'id_workforce_dev_fund']
['idsui', 'id_unemployment_company', 'id_workforce_dev_fund']
数据类型
确保你要相加的所有列都是数值型。注意,我为这些步骤创建了一个子集df(suta_df),以便仅关注现有列。
suta_df = df[existing_cols]
suta_df.dtypes
输出
idsui float64
id_unemployment_company float64
id_workforce_dev_fund float64
dtype: object
数据检查
在求和之前检查存在的数据。下方将提供计数、均值和范围等信息。
suta_df.describe()
输出
| idsui | id_unemployment_company | id_workforce_dev_fund | |
|---|---|---|---|
| 计数 | 4 | 4 | 4 |
| 均值 | 66.25 | 13.75 | 8.75 |
| 标准差 | 13.7689 | 4.78714 | 2.98608 |
| 最小值 | 50 | 10 | 5 |
| 25% | 57.5 | 10 | 7.25 |
| 50% | 67.5 | 12.5 | 9 |
| 75% | 76.25 | 16.25 | 10.5 |
| 最大值 | 80 | 20 | 12 |
缺失数据
在求和之前检查缺失的数据。Des
suta_df.isnull().sum()
输出
idsui 1
id_unemployment_company 1
id_workforce_dev_fund 1
dtype: int64
将求和列加回原始df
df["total_suta"] = suta_df.sum(axis=1)
输出
| id_ast | idsui | id_wd | id_unemployment_company | id_workforce_dev_fund | total_suta | |
|---|---|---|---|---|---|---|
| 0 | 100 | 50 | 25 | 10 | 5 | 65 |
| 1 | 200 | nan | 30 | 20 | nan | 20 |
| 2 | nan | 75 | 45 | 15 | 10 | 100 |
| 3 | 150 | 80 | nan | 10 | 8 | 98 |
| 4 | 175 | 60 | 35 | nan | 12 | 72 |
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。