To Iterate dataframe to check the unit and convert the units .TypeError: tuple indices must be integers or slices, not str
To Iterate dataframe to check the unit and convert the units .TypeError: tuple indices must be integers or slices, not str
我正在尝试检查我的数据集中的单位但出现此错误,有人可以帮助我解决这个问题吗
df=pd.read_csv("/content/unit_checkv1.csv")
UNITS ={"G/HA":0.001,"ML/100KG":0.001}
for row in df.iterrows():
unit_factor, unit_target = UNITS[row["DOSAGE_UNIT"]]
if unit_factor:
row["DOSAGE"] = (
row["DOSAGE"] * unit_factor
if row["DOSAGE"] is not None
else None
)
row["DOSAGE_UNIT"] = unit_target
您可以使用 Series.map
代替您的解决方案:
df['NEW'] = df["DOSAGE_UNIT"].map(UNITS) * df['DOSAGE']
我正在尝试检查我的数据集中的单位但出现此错误,有人可以帮助我解决这个问题吗
df=pd.read_csv("/content/unit_checkv1.csv")
UNITS ={"G/HA":0.001,"ML/100KG":0.001}
for row in df.iterrows():
unit_factor, unit_target = UNITS[row["DOSAGE_UNIT"]]
if unit_factor:
row["DOSAGE"] = (
row["DOSAGE"] * unit_factor
if row["DOSAGE"] is not None
else None
)
row["DOSAGE_UNIT"] = unit_target
您可以使用 Series.map
代替您的解决方案:
df['NEW'] = df["DOSAGE_UNIT"].map(UNITS) * df['DOSAGE']