简体   繁体   English

Pandas DataFrame:减去字符串数据类型的列

[英]Pandas DataFrame: Subtract columns with string datatype

How can I subtract two columns that contain values of type string?如何减去包含字符串类型值的两列? No values are indicated by '---' and should lead to a '---' in the result. '---' 没有表示任何值,结果中应该有一个 '---'。 The result should also be of value type string.结果也应该是值类型字符串。

Source来源

df1 = pd.DataFrame({'x': ['a', 'b', 'c'], 'y': ['5', '---', '7']})

    x   y
0   'a' '5'
1   'b' '---'
2   'c' '7'

df2 = pd.DataFrame({'x': ['a', 'b', 'c'], 'y': ['1', '2', '---']})

    x    y
0   'a'  '1'
1   'b'  '2'
2   'c'  '---'

Target目标

df3 = df1 - df2

    x   y
0   'a' '4'
1   'b' '---'
2   'c' '---'

Try with:尝试:

df1.set_index('x').apply(lambda x: pd.to_numeric(x,errors='coerce')).sub(
      df2.set_index('x').apply(lambda x: pd.to_numeric(x,errors='coerce'))).fillna('--')\
                                                                .reset_index()

   x   y
0  a   4
1  b  --
2  c  --

You could use pd.to_numeric to both replace all '---' to NaNs , and also also cast all values to floats :您可以使用pd.to_numeric将所有'---'替换为NaNs ,也可以将所有值转换为floats

df1['y'] = pd.to_numeric(df1['y'], errors='coerce')
df2['y'] = pd.to_numeric(df2['y'], errors='coerce')

The simply subtract both columns and store the result in df1 for instance:例如,简单地减去两列并将结果存储在df1中:

df1['y'] = (df1['y'] - df2['y']).replace(np.nan,'---')

   x    y
0  a    4
1  b  ---
2  c  ---

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

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