hello i have got a situation here. got dataset and in several 'price' columns there are two values instead of one. looks like "$1.99 64.78" etc.. data look like this
my code looks like
no_na['Kaina'] = no_na['price'].str.split(' ').astype('str')
no_na
this creates new column and adds list of prices. the question is how to add only one element from list to a new column? tried to use pop but got errors
well it worked with
no_na['Kaina'] = no_na['price'].apply(lambda x: x[0:5]).astype(float)
no_na
but still i believe there should be a better way, because if price is 111.11 the result would be 111.1 i guess. now looks like this
CodePudding user response:
I give an example on my data. A dataframe is created and the first element is taken through the slice. The apply function is used to retrieve a value from a list.
import pandas as pd
df = pd.DataFrame({'aaa':[1, 5, 7, 10, 15],
'bbb':['16.99 64.78', '16.99 18.99',
'111', '5', '15.74 555']})
df['ddd'] = df['bbb'].str.split(' ').str[:1].apply(lambda a: a[0])
print(df)
Output
aaa bbb ddd
0 1 16.99 64.78 16.99
1 5 16.99 18.99 16.99
2 7 111 111
3 10 5 5
4 15 15.74 555 15.74
CodePudding user response:
You can take your first item on your column with this formula:
no_na['price'] = no_na['kaina'].apply(lambda x: x[0])
no_na
your dataframe will be like this
if you want float, you only need to remove "$" and change the type
no_na['price'] = no_na['kaina'].apply(lambda x: x[0]).str.replace("$","").astype(float)
no_na