1

Suppose I am trying add rows to a dataframe with 40 columns. Ordinarily it would be something like this:

df = pandas.DataFrame(columns = 'row1', 'row2', ... ,'row40'))

df.loc[0] = [value1, value2, ..., value40]

(don't take the dots literally)

However let's say value1 to value10 are in list1 (i.e. list1 = [value1, value2, ..., value10]), and all the remaining values are individual elements.

I tried:

df.loc[0] = [list1, value11, value12, ..., value40]

but it doesn't work, because Python interprets the entire list as one element. So it's the wrong dimension. How can I rewrite the code to make it work?

1 Answer 1

2

I think you need:

df.loc[0] = list1 + [value11, value12, ..., value40]

Sample:

df = pd.DataFrame(columns = ['row1', 'row2','row3', 'row4','row5', 'row6','row40'])

list1 =  ['a','b','c']
df.loc[0] = list1 + ['d','e','f', 'g']
print (df)
  row1 row2 row3 row4 row5 row6 row40
0    a    b    c    d    e    f     g
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.