Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lambda functions
#3
Thank you for your answer.
I selected the relevant column of the dataframe with:

x = df['field']
where x is:
x
Out[27]:
0 about me:<br />\n<br />\ni would love to think...
1 i am a chef: this is what that means.<br />\n1...
2 i'm not ashamed of much, but writing public te...
3 i work in a library and go to school. . . read...
4 hey how's it going? currently vague on the pro...
5 i'm an australian living in san francisco, but...

and pass it to your suggested function:

avg_words_len_in_str = lambda s: [len(k) / v for k,v in Counter(s.lower().split()).items()]
avg_words_len_in_str(x)[/icode]

but I got:
avg_words_len_in_str(x)
Traceback (most recent call last):

File "<ipython-input-28-425c839e1d1d>", line 1, in <module>
avg_words_len_in_str(x)

File "<ipython-input-21-78c2dd2340ad>", line 1, in <lambda>
avg_words_len_in_str = lambda s: [len(k) / v for k,v in Counter(s.lower().split()).items()]`

File "~/.local/lib/python3.6/site-packages/pandas/core/generic.py", line 5057, in __getattr__
return object.__getattribute__(self, name)

AttributeError: 'Series' object has no attribute 'lower'

Same thing when I applied:
count_spec_words = lambda s, specific: [v for k, v in Counter(s.lower().split()).items() if k in specific]
count_spec_words(x, ("I", ))
count_spec_words(x, ("i",))
count_spec_words(x, ("i"))
Reply


Messages In This Thread
lambda functions - by Gigux - Feb-14-2019, 01:26 PM
RE: lambda functions - by scidam - Feb-15-2019, 12:18 AM
RE: lambda functions - by Gigux - Feb-15-2019, 09:10 AM
RE: lambda functions - by scidam - Feb-17-2019, 06:15 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020