Work With A Row In A Pandas Dataframe Without Incurring Chain Indexing (not Coping Just Indexing)
My data is organized in a dataframe: import pandas as pd import numpy as np data = {'Col1' : [4,5,6,7], 'Col2' : [10,20,30,40], 'Col3' : [100,50,-30,-50], 'Col4' : ['AAA', 'BBB',
Solution 1:
This should work:
row_of_interest = df.loc['R2', :]
row_of_interest.is_copy = False
row_of_interest['Col2'] = row_of_interest['Col2'] + 1000
Setting .is_copy = False
is the trick
Edit 2:
import pandas as pd
import numpy as np
data = {'Col1' : [4,5,6,7], 'Col2' : [10,20,30,40], 'Col3' : [100,50,-30,-50], 'Col4' : ['AAA', 'BBB', 'AAA', 'CCC']}
df = pd.DataFrame(data=data, index = ['R1','R2','R3','R4'])
row_of_interest = df.loc['R2']
row_of_interest.is_copy = False
new_cell_value = row_of_interest['Col2'] + 1000
row_of_interest['Col2'] = new_cell_value
print row_of_interest
df.loc['R2'] = row_of_interest
print df
df:
Col1 Col2 Col3 Col4
R1 4 10 100 AAA
R2 5 1020 50 BBB
R3 6 30 -30 AAA
R4 7 40 -50 CCC
Solution 2:
You can remove the warning by creating a series with the slice you want to work on:
from pandas import Series
row_of_interest = Series(data=df.loc['R2', :])
row_of_interest.loc['Col2'] += 1000print(row_of_interest)
Results in:
Col15Col21020Col350Col4BBBName:R2,dtype:object
Post a Comment for "Work With A Row In A Pandas Dataframe Without Incurring Chain Indexing (not Coping Just Indexing)"