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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With