Reputation: 23
I have problem with speed of my algorithm, is too slow. I have a big dataframe and wanna create columns depends on the name and value in other. I am looking for a solution maybe in Pandas. Before running I don't know the size of the future columns. Here is a simple schema.
"column"<==>"value"<br>"column"<==> "value"<br>...
my data frame
id | params |
---|-----------------
0 |currency<=>PLN<br>price<=>72.14<br>city<==>Berlin
---|-----------------
1 |price<=>90<br>area<=>72.14<br>city<==>San Francisco<br>rooms<==>2<br>is_Free<==>1
---|-----------------
And i would like to have something like this
id | price | currency | city | rooms | is_Free| area|
---|------ |----------|--------------|-------|--------|------
0| 72.14 | PLN | Berlin | NaN | NaN | NaN|
---|-------|----------|--------------|-------|--------|------
1| 90 | NaN | San Francisco| 2 | 1 | 90 |
My solution:
def add_parameters(df):
for i,row in df.iterrows():
parameters_list = row.params.split("<br>")
for parameter in parameters_list:
elem_list = parameter.split("<=>")
if elem_list[0] and elem_list[1] != '':
df.loc[i, elem_list[0]] = elem_list[1]
return df
Thanks
Upvotes: 2
Views: 235
Reputation: 561
If you would like to do it in one line with list comp (but i am not sure if it is readable tho):
pattern = re.compile(r"<=*>")
df = df['params'].apply(lambda row: dict([pattern.split(kv) for kv in row.split("<br>")])).apply(pd.Series)
Upvotes: 1
Reputation: 8302
This is one way of approaching the problem.
import re
# handle multiple seperator.
sep = re.compile(r"(<.*>)")
def split(value):
ret = {}
for s in value.split("<br>"):
# search if seperator exists in the string & split based on sep.
if sep.search(s):
split_ = s.split(sep.search(s).group())
ret[split_[0]] = split_[1]
return ret
print(df['params'].apply(lambda x : split(x)).apply(pd.Series))
Output
currency price city area rooms is_Free
0 PLN 72.14 Berlin NaN NaN NaN
1 NaN 90 San Francisco 72.14 2 1
Upvotes: 2