Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create and set an element of a Pandas DataFrame to a list

I have a Pandas DataFrame that I'm creating row-by-row (I know, I know, it's not Pandorable/Pythonic..). I'm creating elements using .loc like so

output.loc[row_id, col_id]

and I'd like to set this value to an empty list, [].

output.loc[row_id, col_id] = []

Unfortunately, I get an error saying the size of my keys and values do not match (Pandas thinks I'm trying to set values with not to an iterable).

Is there a way to do this?

Thanks!

like image 670
DrMisha Avatar asked Sep 09 '14 18:09

DrMisha


People also ask

How do you turn a DataFrame into a list?

Use pandas. DataFrame() constructor to convert a list to a DataFrame. Use pandas. DataFrame(data, columns) to convert a list to a DataFrame.

How do I append a pandas DataFrame to a list?

Using loc[] to Append The New List to a DataFrame. By using df. loc[index]=list you can append a list as a row to the DataFrame at a specified Index, In order to add at the end get the index of the last record using len(df) function.

Can a list be an element of a DataFrame?

The pandas DataFrame can be created by using the list of lists, to do this we need to pass a python list of lists as a parameter to the pandas. DataFrame() function. Pandas DataFrame will represent the data in a tabular format, like rows and columns.


2 Answers

You need to make sure two things:

  1. there is precisely one entry for that loc,
  2. the column has dtype object (actually, on testing this seems not to be an issue).

A hacky way to do this is to use a Series with []:

In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])

In [12]: df.loc[[0], 'A'] = pd.Series([[]])

In [13]: df
Out[13]:
    A  B
0  []  2
1   3  4

pandas doesn't really want you use [] as elements because it's usually not so efficient and makes aggregations more complicated (and un-cythonisable).


In general you don't want to build up DataFrames cell-by-cell, there is (almost?) always a better way.

like image 103
Andy Hayden Avatar answered Oct 21 '22 10:10

Andy Hayden


The answer by MishaTeplitskiy works when the index label is 0. More generally, if you want to assign an array x to an element of a DataFrame df with row r and column c, you can use:

df.loc[[r], c] = pd.Series([x], index = [r])
like image 42
Misha Avatar answered Oct 21 '22 09:10

Misha