Reputation: 1319
I would like more info. on the answer to the following question:
df = pd.DataFrame(['aa', 'bb', 'xx', 'uu'], [21, 16, 50, 33], columns = ['Name', 'Age'])
Choose the correct option:
I found more than one answer online but not sure. I think the answer is number 2 but when i tried x = df['name'] then x[0] = 'cc' then print(df) I saw that the change appeared in the original dataframe. So how the changed appeared in the original dataframe although I also got this warining: A value is trying to be set on a copy of a slice from a DataFrame
I just want to know more about the difference between the two and weather one is really a copy of the original dataframe or not. Thank you.
Upvotes: 0
Views: 2194
Reputation: 323236
Both of them are VIEW
only, if you need a copy we need
df['Name'].copy().
df.loc[:, 'Name'].copy()
loc
here can give you access to index
and column
at the same time, also avoid chain slice
df.loc[cond1, cond2]
Notice above loc
can also pass condition rather than only column names
df.loc[:, df.columns=='Name']
#df[df.columns=='Name'] this will return error
Example for Div
df=pd.DataFrame([[1,2,3],[2,3,4]])
df
0 1 2
0 1 2 3
1 2 3 4
df[df.columns==0]
Traceback (most recent call last):
Upvotes: 2
Reputation: 2721
Both are the views of original dataframe One can be used to add more columns in dataframe and one is used for specifically getting a view of a cell or row or column in dataframe.
Upvotes: 0