Q. Write statement(s) to change the values to 750 at 4th row to 9th row, 7th column in a DataFrame df.



Answer :

import pandas as pd

d={'c1':['R1C1', 'R2C1', 'R3C1', 'R4C1', 'R5C1', 'R6C1', 'R7C1', 'R8C1', 'R9C1', 'R10C1'],\
   'c2':['R1C2', 'R2C2', 'R3C2', 'R4C2', 'R5C2', 'R6C2', 'R7C2', 'R8C2', 'R9C2', 'R10C2'],\
 'c3':['R1C3', 'R2C3', 'R3C3', 'R4C3', 'R5C3', 'R6C3', 'R7C3', 'R8C3', 'R9C3', 'R10C3'],\
   'c4':['R1C4', 'R2C4', 'R3C4', 'R4C4', 'R5C4', 'R6C4', 'R7C4', 'R8C4', 'R9C4', 'R10C4'],\
   'c5':['R1C5', 'R2C5', 'R3C5', 'R4C5', 'R5C5', 'R6C5', 'R7C5', 'R8C5', 'R9C5', 'R10C5'],\
   'c6':['R1C6', 'R2C6', 'R3C6', 'R4C6', 'R5C6', 'R6C6', 'R7C6', 'R8C6', 'R9C6', 'R10C6'],\
   'c7':['R1C7', 'R2C7', 'R3C7', 'R4C7', 'R5C7', 'R6C7', 'R7C7', 'R8C7', 'R9C7', 'R10C7'],\
   'c8':['R1C8', 'R2C8', 'R3C8', 'R4C8', 'R5C8', 'R6C8', 'R7C8', 'R8C8', 'R9C8', 'R10C8']}

Method 1:-

df=pd.DataFrame(d)
print('old df')
print(df)
print('new df')
df ["c7"] [3:9] = 750 # c7 is Name of 7th column in a DataFrame df
print(df)

Output :-

Old Dataframe:-

      c1           c2         c3         c4     c5          c6           c7     c8
0   R1C1   R1C2   R1C3   R1C4   R1C5   R1C6   R1C7   R1C8
1   R2C1   R2C2   R2C3   R2C4   R2C5   R2C6   R2C7   R2C8
2   R3C1   R3C2   R3C3   R3C4   R3C5   R3C6   R3C7   R3C8
3   R4C1   R4C2   R4C3   R4C4   R4C5   R4C6   R4C7   R4C8
4   R5C1   R5C2   R5C3   R5C4   R5C5   R5C6   R5C7   R5C8
5   R6C1   R6C2   R6C3   R6C4   R6C5   R6C6   R6C7   R6C8
6   R7C1   R7C2   R7C3   R7C4   R7C5   R7C6   R7C7   R7C8
7   R8C1   R8C2   R8C3   R8C4   R8C5   R8C6   R8C7   R8C8
8   R9C1   R9C2   R9C3   R9C4   R9C5   R9C6   R9C7   R9C8
9  R10C1  R10C2  R10C3  R10C4  R10C5  R10C6  R10C7  R10C

New Dataframe :-

       c1           c2         c3         c4     c5          c6           c7     c8
0   R1C1   R1C2   R1C3   R1C4   R1C5   R1C6   R1C7   R1C8
1   R2C1   R2C2   R2C3   R2C4   R2C5   R2C6   R2C7   R2C8
2   R3C1   R3C2   R3C3   R3C4   R3C5   R3C6   R3C7   R3C8
3   R4C1   R4C2   R4C3   R4C4   R4C5   R4C6    750   R4C8
4   R5C1   R5C2   R5C3   R5C4   R5C5   R5C6    750   R5C8
5   R6C1   R6C2   R6C3   R6C4   R6C5   R6C6    750   R6C8
6   R7C1   R7C2   R7C3   R7C4   R7C5   R7C6    750   R7C8
7   R8C1   R8C2   R8C3   R8C4   R8C5   R8C6    750   R8C8
8   R9C1   R9C2   R9C3   R9C4   R9C5   R9C6    750   R9C8
9  R10C1  R10C2  R10C3  R10C4  R10C5  R10C6  R10C7  R10C8

Method 2:-

print('old df')
print(df)
print()
print('new df')
df.loc[3:8, 'c7'] = 750  # c7 is Name of 7th column in a DataFrame df
print(df)

Output :-

Old Dataframe :-

       c1           c2         c3         c4     c5          c6           c7     c8
0   R1C1   R1C2   R1C3   R1C4   R1C5   R1C6   R1C7   R1C8
1   R2C1   R2C2   R2C3   R2C4   R2C5   R2C6   R2C7   R2C8
2   R3C1   R3C2   R3C3   R3C4   R3C5   R3C6   R3C7   R3C8
3   R4C1   R4C2   R4C3   R4C4   R4C5   R4C6   R4C7   R4C8
4   R5C1   R5C2   R5C3   R5C4   R5C5   R5C6   R5C7   R5C8
5   R6C1   R6C2   R6C3   R6C4   R6C5   R6C6   R6C7   R6C8
6   R7C1   R7C2   R7C3   R7C4   R7C5   R7C6   R7C7   R7C8
7   R8C1   R8C2   R8C3   R8C4   R8C5   R8C6   R8C7   R8C8
8   R9C1   R9C2   R9C3   R9C4   R9C5   R9C6   R9C7   R9C8
9  R10C1  R10C2  R10C3  R10C4  R10C5  R10C6  R10C7  R10C8

New Datafram :-

       c1           c2         c3         c4     c5          c6           c7     c8
0   R1C1   R1C2   R1C3   R1C4   R1C5   R1C6   R1C7   R1C8
1   R2C1   R2C2   R2C3   R2C4   R2C5   R2C6   R2C7   R2C8
2   R3C1   R3C2   R3C3   R3C4   R3C5   R3C6   R3C7   R3C8
3   R4C1   R4C2   R4C3   R4C4   R4C5   R4C6    750   R4C8
4   R5C1   R5C2   R5C3   R5C4   R5C5   R5C6    750   R5C8
5   R6C1   R6C2   R6C3   R6C4   R6C5   R6C6    750   R6C8
6   R7C1   R7C2   R7C3   R7C4   R7C5   R7C6    750   R7C8
7   R8C1   R8C2   R8C3   R8C4   R8C5   R8C6    750   R8C8
8   R9C1   R9C2   R9C3   R9C4   R9C5   R9C6    750   R9C8

9  R10C1  R10C2  R10C3  R10C4  R10C5  R10C6  R10C7  R10C8

4 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. There should be [4:9]

    ReplyDelete
  2. No it should be [4:10] because we give stop values one ahead the number

    ReplyDelete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post