1

I want to create a dataframe with one of the column as a list or array But an attempt to do so gives an error 'Must have equal len keys and value when setting with an iterable' Please support

  eg  data.loc[0,'emails']={'[email protected]','[email protected]'} =>error 
      data.loc[0,'emails']='[email protected]' =>No error

Data=>

> Name  | emails
  ___________________________  
> Judas |['[email protected]','[email protected]']
> Priest|['[email protected]','[email protected]','[email protected]']

This scenario could be one like trying to get only list of emails as a column from one of the columns which contains very long texts.

4
  • 1
    What was the attempt that produced that result? Commented Sep 10, 2018 at 15:09
  • Array-likes in DataFrame columns is often a bad idea, why do you want to do it? Commented Sep 10, 2018 at 15:09
  • I am trying to get list of words of interest from one of the columns which contains long text Commented Sep 10, 2018 at 15:13
  • Wouldn't the following structure be better? (Rows) Judas, [email protected] | Judas, [email protected] | Priest, [email protected] | ... Commented Sep 10, 2018 at 15:16

2 Answers 2

3

After you assign a list like or array like value to the columns, the column should be considered as type object

df=pd.DataFrame({'Name':['Juda','Pri']})
df['Email']=''
df.Email=df.Email.astype(object)
df.loc[0,'Email']={'[email protected]','[email protected]'}
df
Out[511]: 
   Name               Email
0  Juda  {[email protected], [email protected]}
1   Pri                    
Sign up to request clarification or add additional context in comments.

Comments

0

The question could be more clear, to be honest. Does this result in what you expect?

a = ["Judas" , ['[email protected]','[email protected]']]
b = ['Priest', ['[email protected]','[email protected]','[email protected]']]
df = pandas.DataFrame([a,b])
print(df)

gives:

            0                            1
0   Judas           [[email protected], [email protected]]
1  Priest  [[email protected], [email protected], [email protected]]

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.