Pandas 数据修改的问题

  • 表格
  • image.png
  • 希望用pandas解决 当项目编号为1575时,项名1=项名2

尝试用

import pandas as pd
data=pd.read_excel("table")
if data["项目编号"]=="1575":
    data[["项目名称1","项目名称2"]]=data[["项目名称2","项目名称1"]]

会提示报错。
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

阅读 1.4k
1 个回答

我挺好奇你pandas导入名是啥。

# 1
for i in df.index:
    if df.loc[i,"项目编号"]=="1575":
        df.loc[i,"项目名称1"]=df.loc[i,"项目名称2"]

# 2
df["项目名称1"]=df.apply(lambda x: x["项目名称2"] if x["项目编号"]=="1575" else x["项目名称1"],axis=1)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进