I have this column with some cells that have two values, first value is price after discount and second value is price after discount. I need to separate these values into two cells before and after discount I have tried the following methods but neither of them works so if anyone have any suggestions how to make or how to edit the code to do it.
Column Example:
Price
row 1: 79.9,99.9
row 2: 59.9
row 3: 49.9,89.9
row 4: 59.9
Method 1:
for z in x['1Month Price']:
x['1Month Price'].split(',')
Error:
AttributeError Traceback (most recent call last)
<ipython-input-32-1b04b6fed466> in <module>
----> 1 x['1_Month_bf'] = x['1Month Price'].split(',', expand = True)
2 x.head()
~\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5463 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5464 return self[name]
-> 5465 return object.__getattribute__(self, name)
5466
5467 def __setattr__(self, name: str, value) -> None:
AttributeError: 'Series' object has no attribute 'split'
Method 2:
x['1_Month_bf'] = x['1Month Price'].apply(lambda z: z.split(',')[0])
x.head()
Error:
AttributeError Traceback (most recent call last)
<ipython-input-33-aa7ce98376af> in <module>
----> 1 x['1_Month_bf'] = x['1Month Price'].apply(lambda z: z.split(',')[0])
2 x.head()
~\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
4136 else:
4137 values = self.astype(object)._values
-> 4138 mapped = lib.map_infer(values, f, convert=convert_dtype)
4139
4140 if len(mapped) and isinstance(mapped[0], Series):
pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()
<ipython-input-33-aa7ce98376af> in <lambda>(z)
----> 1 x['1_Month_bf'] = x['1Month Price'].apply(lambda z: z.split(',')[0])
2 x.head()
AttributeError: 'int' object has no attribute 'split'
CodePudding user response:
Try with str.split
. First convert each rows as string to avoid the 2nd error then split your strings then convert them as float.
df[['discount', 'price']] = (
df['1Month Price'].astype(str).str.split(',', expand=True).astype(float)
)
print(df)
# Output
1Month Price discount price
0 79.9,99.9 79.9 99.9
1 59.9 59.9 NaN
2 49.9,89.9 49.9 89.9
3 59.9 59.9 NaN
CodePudding user response:
df[['Price A', 'Price B']] = df['Price'].str.split(',', expand=True)
Outcome
Price Price A Price B
0 79.9,99.9 79.9 99.9
1 59.9 59.9 None
2 49.9,89.9 49.9 89.9
3 59.9 59.9 None