It's a pretty simple example
import pandas
df = pandas.DataFrame()
value_to_be_set = {'1'}
df.loc[0, 'col1'] = value_to_be_set
df['col2'] = None
df.loc[0, 'col2'] = value_to_be_set
print(df.head())
output
col1 col2
0 1 {1}
Why is the datatype different for both columns?
Python 3.7.3
pandas version: 0.23.4
In first assignment, you create a num_column from a set, said differently from an iterable. You ask for 1 single element and provide an iterable of size one, so you affect the content of the set to the single cell. You can try to use a set of 2 values to see that it would raise an error.
In second assignment, you update a cell in an existing column. Pandas has no reason to unpack anything here, and it affects the set to the cell.
To be honest, this explains what happens, but is not a justification for the rationale behind the different behaviours...
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