Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

python list apply lamda 1

Status
Not open for further replies.

kingz2000

Programmer
May 28, 2002
304
0
0
DE
Hi guys,

I have a list with this command: categories.loc[0,:]

which reads

0 related-1
1 request-0
2 offer-0
3 aid_related-0

Now I would like to append a Dataframe "categories" with these contents, except I don't want the last two symbols.

So the categories dataframe should have columns "related", "request", "offer" and "aid related" as new columns.

I tried this:

row = categories.loc[0,:]

for line in row:
categories=categories.apply[lambda x: line.str.split('-')[0])

which does not work.
Can anyone help?

Kingz
 
if you have lambda of x, then you have to use x inside of it, i.e. x.split("-")[0] (and not line.str.split('-')[0])

see example:
Code:
>>> categories = pd.Series(["related-1", "request-0", "offer-0", "aid_related-0"])
>>> categories.loc[0:]
0        related-1
1        request-0
2          offer-0
3    aid_related-0
dtype: object
>>> categories = categories.apply(lambda x: x.split("-")[0])
>>> categories.loc[0:]
0        related
1        request
2          offer
3    aid_related
dtype: object
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top