0

I have a list consisting of several string as shown below list=['abc','cde','fgh'] I want to add all the values to a particular index of dataframe. I am trying it with the code

df1.ix[1,3]=[list]
df1.to_csv('test.csv', sep=',')

I want in dataframe at poistion 1,3 all values to be inserted as it is ['abc','cde','fgh']. I don't want to convert it to string or any other format. But it is giving me error. what I am doing wrong here

3
  • 1
    What is error? What is df1.info() ? Commented Oct 21, 2016 at 12:05
  • the error is ----raise ValueError('Must have equal len keys and value ' ValueError: Must have equal len keys and value when setting with an ndarray Commented Oct 21, 2016 at 12:07
  • Can you add sample of df1? Please check How to make good reproducible pandas examples Commented Oct 21, 2016 at 12:08

2 Answers 2

3

I think you can use:

df1.ix[1,3] = L

Also is not recommended use variable list, because code word in python.

Sample:

df1 = pd.DataFrame({'a':[1,2,3], 'b':[1,2,3], 'c':[1,2,3], 'd':[1,2,3]})
print (df1)
   a  b  c  d
0  1  1  1  1
1  2  2  2  2
2  3  3  3  3

L = ['abc','cde','fgh'] 
df1.ix[1,3]= L

print (df1)
   a  b  c                d
0  1  1  1                1
1  2  2  2  [abc, cde, fgh]
2  3  3  3                3
Sign up to request clarification or add additional context in comments.

4 Comments

still have this error ---- raise ValueError('Must have equal len keys and value ' ValueError: Must have equal len keys and value when setting with an iterable
It return error with my sample? Or sample works and problem is with real data?
What is your pandas version print pd.show_info() ?
version is 0.19.0
1

I think you meant to use 1:3 not 1,3

consider the pd.DataFrame df

df = pd.DataFrame(dict(A=list('xxxxxxx')))

use loc or ix
df.loc[1:3, 'A'] = ['abc', 'cde', 'fgh']
or
df.ix[1:3, 'A'] = ['abc', 'cde', 'fgh']
yields
enter image description here

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.