Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning a scalar value to an empty DataFrame doesn't appear to do anything

I'm new to pandas and have a very basic question, please!

On Python v3.6 through spyder:

x= pd.DataFrame(columns = ['1','2'])
print(x)
x['1'] = '25'
print(x)

From the print statements, the dataframe x does not appear to change. My question: What does x['1'] = '25' do, if anything?

like image 396
Danny B Avatar asked Jun 14 '19 17:06

Danny B


People also ask

What happens when you assign a value to a column of a DataFrame?

The assign() function is used to assign new columns to a DataFrame. Returns a new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten. The column names are keywords.

How do you assign a value to a data frame?

You can set cell value of pandas dataframe using df.at[row_label, column_label] = 'Cell Value'. It is the fastest method to set the value of the cell of the pandas dataframe. Dataframe at property of the dataframe allows you to access the single value of the row/column pair using the row and column labels.


1 Answers

There is actually a difference between the semantics of assigning scalars and iterables (think containers such as lists as list-like objects).

Consider,

df = pd.DataFrame(columns=['1', '2'])                                                                                             
df                                                                                                                                  

Empty DataFrame
Columns: [1, 2]
Index: []

You've defined an empty dataframe without any index (no rows), but only a schema for the columns.

When you assign a scalar to a column, the assignment is broadcast across all rows. In this case, since there are none, nothing happens:

df['1'] = 123
df

Empty DataFrame
Columns: [1, 2]
Index: []

However, assigning a list-like iterable is a different story, as pandas will create new rows for it:

df['1'] = [123]
df

     1    2
0  123  NaN

Now, to understand how scalar assignment works, consider a similar empty DataFrame, but with a defined index:

df = pd.DataFrame(columns=['1', '2'], index=[0, 1])
df                                                                                                                                  

     1    2
0  NaN  NaN
1  NaN  NaN

it is still "empty" (not really), but now we can assign scalars and the assignment is broadcast,

df['1'] = 123
df                                                                                                                                  

     1    2
0  123  NaN
1  123  NaN

Contrast this behaviour with that previously shown.

like image 68
cs95 Avatar answered Nov 06 '22 15:11

cs95