Python, pandas: how to remove greater than sign -
let's have following example dataframe
from pandas import series, dataframe df = dataframe({'a':['1', '<2', '3']}) i convert column string integer. in case of '<2', i'd take off '<' sign , put 1 (the closest integer less 2) in second row. what's efficient way that? example. actual data i'm working on has hundreds of thousands of rows. in advance.
you use series.apply:
import pandas pd df = pd.dataframe({'a':['1', '<2', '3']}) df['a'] = df['a'].apply(lambda x: int(x[1:])-1 if x.startswith('<') else int(x)) print(df.dtypes) # int64 # dtype: object yields
print(df) 0 1 1 1 2 3 [3 rows x 1 columns]
Comments
Post a Comment