简体   繁体   English

如果相邻单元格中的条件为真,则更新值

[英]Update value if a condition in the adjacent cell is true

I'm using Pandas to read a CSV data file into a DataFrame. 我正在使用Pandas将CSV数据文件读入DataFrame。 The data is basically a bunch of dimensions in one column and the next one is the unit. 数据基本上是一列中的一堆维度,下一列是单位。

IE IE浏览器

  Perameter  | measurement | unit
      Height |     72      |  inches
      Length |     20      |  mm

I would then like to search through the DataFrame, find all places where the unit does not match what it should (ie lbs vs kg) and then simply convert it to the units that the rest of the script uses. 然后,我想搜索DataFrame,找到该单位不符合其应有的所有位置(即lbs vs kg),然后将其转换为脚本其余部分使用的单位。

I'm stumped on how to search through based on one column, read that value, divide/multiply that value by the conversion factor, then replace the value in the DataFrame. 我很困惑如何基于一列进行搜索,读取该值,将该值除以/乘以转换因子,然后替换DataFrame中的值。

I can't get this to work: 我无法使它正常工作:

df = pd.read_csv('path/to/Input.csv')

data.loc[ data.Units == 'in' ] = x
data[measurement][x] = ((data[measurement][x])/25.4) # convert to mm.

Any ideas? 有任何想法吗?

You can do this: 你可以这样做:

# Multiply the measurement by 25.4 where unit is inches
df.loc[df.unit == 'inches', 'measurement'] *= 25.4
# Replace `inches` with `mm` in unit column after it's been converted
df['unit'].replace('inches', 'mm', inplace=True)

>>> df
  Perameter  measurement unit
0    Height       1828.8   mm
1    Length         20.0   mm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM