mj1261829
mj1261829

Reputation: 1319

df['colimn_name'] vs df.loc[:, 'colimn_name']

I would like more info. on the answer to the following question:

  1. df[‘Name’] and 2. df.loc[:, ‘Name’], where:

df = pd.DataFrame(['aa', 'bb', 'xx', 'uu'], [21, 16, 50, 33], columns = ['Name', 'Age'])

Choose the correct option:

  1. 1 is the view of original dataframe and 2 is a copy of original
    dataframe
  2. 2 is the view of original dataframe and 1 is a copy of original dataframe
  3. Both are copies of original dataframe
  4. Both are views of original dataframe

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

Answers (2)

BENY
BENY

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

Divyessh
Divyessh

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

Related Questions