0

Hi I have a table like this

id q         
1  hello?     
2  helloWorld 
3  hi         

I want to create another column with name 'k' like this,

id q              k
1  hello?        [h,e,l,o]
2  helloWorld    [h,e,l,o,w,r,d]
3  hi            [h, i]

The type of k row is nparray, is this possible with Pandas?

5
  • 1
    if you already have the rows of k stored somehow, like in a variable kData, then you'd just do existing_dataframe['k'] = kData. However, it's unclear what you have in memory and what you want to do. Are you generating the rows of k from the existing dataframe? Commented May 22, 2021 at 1:10
  • I have a kValue = [h, e, l, o] , I want to insert something like existing_dataframe['k'][0] = kValue Commented May 22, 2021 at 1:18
  • 1
    Then your kValues should be included in the body of your question. Commented May 22, 2021 at 1:19
  • I think I was able to do this somehow, thanks for the answers, though it is helpful for me Commented May 22, 2021 at 1:21
  • it is just an example, I thought panda rows doesn't accept array values, but apparently it do Commented May 22, 2021 at 1:22

1 Answer 1

3

Try filtering out non-alphabetical characters with replace then apply + pd.unqiue:

import pandas as pd

df = pd.DataFrame({
    'id': [1, 2, 3],
    'q': ['hello?', 'helloWorld', 'hi']
})

df['k'] = df['q'].replace(r'\W', '', regex=True) \
    .apply(lambda x: pd.unique(list(x.lower())))

print(df)

df:

   id           q                      k
0   1      hello?           [h, e, l, o]
1   2  helloWorld  [h, e, l, o, w, r, d]
2   3          hi                 [h, i]

Or if order doesn't matter set is an option:

import numpy as np
import pandas as pd

df = pd.DataFrame({
    'id': [1, 2, 3],
    'q': ['hello?', 'helloWorld', 'hi']
})

df['k'] = df['q'].replace(r'\W', '', regex=True) \
    .apply(lambda x: np.array([*set(x.lower())]))

print(df)

df:

   id           q                      k
0   1      hello?           [h, o, l, e]
1   2  helloWorld  [o, e, r, d, h, l, w]
2   3          hi                 [i, h]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.