Here is a simple pandas Dataframe defined as follow:
df = pd.DataFrame( {
'word': ['flower', 'mountain', 'ocean', 'universe'],
'k': [1, 2, 3, 4]
} )
>>> df
k word
0 1 flower
1 2 mountain
2 3 ocean
3 4 universe
I want to change df into this ( replace every word with its first k letters )
>>> df
k word
0 1 f
1 2 mo
2 3 oce
3 4 univ
I have an idea to achieve this by using pandas.Series.apply with a custom function
def get_first_k_letters( x, k ):
return x[:k]
df['word'] = df['word'].apply( get_first_k_letters, args=(3,) )
>>> df
k word
0 1 flo
1 2 mou
2 3 oce
3 4 uni
I can easily replace every word with its first 3 letters by setting args=(3,).
But I want to replace every word with its first k letters ( k is not always the same ) and I don't know what is the setting for args in this case.
Could somebody help me? Thanks! ( Other methods without using pandas.Series.apply will also be OK! )