I trying to modify the value of just a single columns in a pandas dataframe, however, when I apply the map() function, another unrelated column also changes its values. How can I modify just a single column?
columns = [[[776, 114, 67, 47], [776, 167, 67, 52], [776, 224, 67, 62],[776, 291, 67, 58],[776, 355, 67, 52]],
[[849, 114, 76, 48], [849, 167, 76, 52],[849, 225, 76, 61],[849, 291, 76, 59],[849, 355, 75, 53]],
[[930, 115, 113, 47], [930, 168, 113, 51], [930, 225, 113, 61], [930, 292, 113, 58], [930, 355, 113, 53]]]
dic = {'A': columns[0], 'B': columns[0], 'C': columns[1], 'D': columns[2]}
df = pd.DataFrame.from_dict(dic)
def subs(x):
x[0] = 230
x[2] = 533
return x
df['A'] = df['A'].map(subs)
A B C D
0 [230, 114, 533, 47] [230, 114, 533, 47] [849, 114, 76, 48] [930, 115, 113, 47]
1 [230, 167, 533, 52] [230, 167, 533, 52] [849, 167, 76, 52] [930, 168, 113, 51]
2 [230, 224, 533, 62] [230, 224, 533, 62] [849, 225, 76, 61] [930, 225, 113, 61]
3 [230, 291, 533, 58] [230, 291, 533, 58] [849, 291, 76, 59] [930, 292, 113, 58]
4 [230, 355, 533, 52] [230, 355, 533, 52] [849, 355, 75, 53] [930, 355, 113, 53]
I need it to return as follows:
A B C D
0 [230, 114, 533, 47] [776, 114, 67, 47] [849, 114, 76, 48] [930, 115, 113, 47]
1 [230, 167, 533, 52] [776, 167, 67, 52] [849, 167, 76, 52] [930, 168, 113, 51]
2 [230, 224, 533, 62] [776, 224, 67, 62] [849, 225, 76, 61] [930, 225, 113, 61]
3 [230, 291, 533, 58] [776, 291, 67, 58] [849, 291, 76, 59] [930, 292, 113, 58]
4 [230, 355, 533, 52] [776, 355, 67, 52] [849, 355, 75, 53] [930, 355, 113, 53]
CodePudding user response:
A and B use the same underlying lists.
Use deepcopy(columns[0])
to make it independent:
from copy import deepcopy
dic = {'A': columns[0], 'B': deepcopy(columns[0]), 'C': columns[1], 'D': columns[2]}
df = pd.DataFrame.from_dict(dic)
Note that this still modifies columns
, if you want to avoid this use deepcopy
for all columns.
Output:
A B C D
0 [230, 114, 533, 47] [776, 114, 67, 47] [849, 114, 76, 48] [930, 115, 113, 47]
1 [230, 167, 533, 52] [776, 167, 67, 52] [849, 167, 76, 52] [930, 168, 113, 51]
2 [230, 224, 533, 62] [776, 224, 67, 62] [849, 225, 76, 61] [930, 225, 113, 61]
3 [230, 291, 533, 58] [776, 291, 67, 58] [849, 291, 76, 59] [930, 292, 113, 58]
4 [230, 355, 533, 52] [776, 355, 67, 52] [849, 355, 75, 53] [930, 355, 113, 53]