Fill-Random-Marks

Sun 29 June 2025
import numpy as np
import pandas as pd
datatype = [('Science', 'int32'), ('Maths', 'int32')]
current_values = np.zeros(3, dtype=datatype)
current_index = ['Row '+str(i) for i in range(1, 4)]
df = pd.DataFrame(current_values, index=current_index)
df
Science Maths
Row 1 0 0
Row 2 0 0
Row 3 0 0
df['Science']
Row 1    0
Row 2    0
Row 3    0
Name: Science, dtype: int32
df['Science'][0] = 45
C:\Users\Afia Jahan\AppData\Local\Temp\ipykernel_25452\2542361963.py:1: FutureWarning: Series.__setitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To set a value by position, use `ser.iloc[pos] = value`
  df['Science'][0] = 45
df['Maths'][1] = 100
C:\Users\Afia Jahan\AppData\Local\Temp\ipykernel_25452\981075704.py:1: FutureWarning: ChainedAssignmentError: behaviour will change in pandas 3.0!
You are setting values through chained assignment. Currently this works in certain cases, but when using Copy-on-Write (which will become the default behaviour in pandas 3.0) this will never work to update the original DataFrame or Series, because the intermediate object on which we are setting values will behave as a copy.
A typical example is when you are setting values in a column of a DataFrame, like:

df["col"][row_indexer] = value

Use `df.loc[row_indexer, "col"] = values` instead, to perform the assignment in a single step and ensure this keeps updating the original `df`.

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

  df['Maths'][1] = 100
C:\Users\Afia Jahan\AppData\Local\Temp\ipykernel_25452\981075704.py:1: FutureWarning: Series.__setitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To set a value by position, use `ser.iloc[pos] = value`
  df['Maths'][1] = 100
df
Science Maths
Row 1 45 0
Row 2 0 100
Row 3 0 0
df.shape
(3, 2)


Score: 10

Category: basics