56

I want to create a python pandas DataFrame with a single row, to use further pandas functionality like dumping to *.csv.

I have seen code like the following being used, but I only end up with the column structure, but empty data

import pandas as pd

df = pd.DataFrame()
df['A'] = 1
df['B'] = 1.23
df['C'] = "Hello"
df.columns = [['A','B','C']]

print df

Empty DataFrame
Columns: [A, B, C]
Index: []

While I know there are other ways to do it (like from a dictionary), I want to understand why this piece of code is not working for me!? Is this a version issue? (using pandas==0.19.2)

5
  • 5
    You need two wrap the first one in brackets: df['A'] = [1] Commented Aug 4, 2017 at 10:22
  • 2
    Or, mention the first row, df.loc[0, 'A'] = 1? Commented Aug 4, 2017 at 10:25
  • 4
    alternatively, if you add df = pd.DataFrame(index=[0]) - then your code should work properly Commented Aug 4, 2017 at 10:28
  • @MaxU this indeed makes the above code runnable as it is. But the code as posted above is not runnable itself (or at least creates empty dataframs), is this correct? So the above code can be considered as buggy!? Commented Aug 7, 2017 at 9:33
  • @HeXor, it depends on the definition of "buggy code"... Pandas allows us to do things in many different ways. IMO the most idiomatic way of creating DFs is to collect the data and then create DF at once compared to adding single rows, which is usually pretty inefficient... Commented Aug 7, 2017 at 9:44

4 Answers 4

81
In [399]: df = pd.DataFrame(columns=list('ABC'))

In [400]: df.loc[0] = [1,1.23,'Hello']

In [401]: df
Out[401]:
   A     B      C
0  1  1.23  Hello

or:

In [395]: df = pd.DataFrame([[1,1.23,'Hello']], columns=list('ABC'))

In [396]: df
Out[396]:
   A     B      C
0  1  1.23  Hello
Sign up to request clarification or add additional context in comments.

Comments

7

If you are creating the DataFrame from a dict you can do the following:

>>> data = dict(A=1, B=1.23, C='Hello')
>>> df = pd.DataFrame(data, index=[0])
>>> df
   A     B      C
0  1  1.23  Hello

Comments

0

I know this is an old post, but based on this explanation in this post Creating an empty Pandas DataFrame, then filling it?

We should not be doing dataframe adding. I am asking this because I am doing something similar but I opted for the solution in the post I shared over this one.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

I ended here because I want to create a single-row Pandas DataFrame from a numerical list (or NumPy array) but got a df with a single column. @ayhan's first comment was what I needed:

  import pandas as pd
  lst = [1,2,3]
  df = pd.DataFrame(lst)    # df with a single column
  df = pd.DataFrame([lst])  # df with a single row

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.