1

I would like to initialize a pandas dataframe with column titles and one row of data. I would then like to append rows of data onto the end of this dataframe as I iterate through a file that I am reading the data from.

Here is an example of what I am doing and the issue that I am running into:

titles = ['a','b','c','d']
data = ['1','2','3','4']
df = pd.DataFrame(data, columns=titles)

ValueError: Shape of passed values is (4, 1), indices imply (4, 4)

I know that this is because the data is being inserted as one column instead of one row. How can I make sure the data is inserted as a row?

What is the best way to append additional rows of data onto the end of this dataframe?

1
  • 3
    df = pd.DataFrame([data], columns=titles) Commented Jul 20, 2020 at 16:48

1 Answer 1

1

When you give a one-dimensional list, it takes as if there is only single column, but here you are having multiple columns, that is the reason you are getting error.

So when you have multiple columns we should input a two-dimensional list, as:

df = pd.DataFrame([data], columns = titles)
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.