简体   繁体   中英

Subtracting a string from a string in a Python Pandas dataframe

This is my dataframe:

    date item1 item2  item1 birth  item2 birth  item1 age  item2 age
0   1980     a     f         1975         1979          5          1
1   1980     a     f         1975         1979          5          1
2   1979     e     f         1979         1979          0          0
3   1979     e     f         1979         1979          0          0
4   1978     c     d         1976         1978          2          0
5   1977     a     b         1975         1975          2          2
6   1977     a     b         1975         1975          2          2
7   1975     a     b         1975         1975          0          0
8   1975     a     b         1975         1975          0          0
9   1977     b     a         1975         1975          2          2
10  1976     b     c         1975         1976          1          0

[11 rows x 7 columns]

When doing this: df2 = df[ df['date' - 'item1 birth'] <= 3 ]

I get this: TypeError: unsupported operand type(s) for -: 'str' and 'str'

When instead going: int('date') - int('item1 birth')

I get this: ValueError: invalid literal for int() with base 10: 'date'

And when doing: float('date') - float('item1 birth')

I end up with: ValueError: could not convert string to float: date

Any ideas?

Assuming the column values of numeric dtype, you could subtract the column values this way:

mask = (df['date'] - df['item1 birth'] <= 3)
df2 = df.loc[mask]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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