What is difference between DataFrame attribute and column [duplicate]
This question already has an answer here:
In pandas, what's the difference between df['column'] and df.column?
1 answer
In [66]: data
Out[66]:
col1 col2 label
0 1.0 a c
1 2.0 b d
2 3.0 c e
3 0.0 d f
4 4.0 e 0
5 5.0 f 0
In [67]: data.label
Out[67]:
0 c
1 d
2 NaN
3 f
4 NaN
5 NaN
Name: col2, dtype: object
In [68]: data['label']
Out[68]:
0 c
1 d
2 e
3 f
4 0
5 0
Name: label, dtype: object
Why data.label and data['label'] showing different results?
python pandas dataframe
marked as duplicate by Mohamed Thasin ah, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 at 20:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
In pandas, what's the difference between df['column'] and df.column?
1 answer
In [66]: data
Out[66]:
col1 col2 label
0 1.0 a c
1 2.0 b d
2 3.0 c e
3 0.0 d f
4 4.0 e 0
5 5.0 f 0
In [67]: data.label
Out[67]:
0 c
1 d
2 NaN
3 f
4 NaN
5 NaN
Name: col2, dtype: object
In [68]: data['label']
Out[68]:
0 c
1 d
2 e
3 f
4 0
5 0
Name: label, dtype: object
Why data.label and data['label'] showing different results?
python pandas dataframe
marked as duplicate by Mohamed Thasin ah, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 at 20:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
In pandas, what's the difference between df['column'] and df.column?
1 answer
In [66]: data
Out[66]:
col1 col2 label
0 1.0 a c
1 2.0 b d
2 3.0 c e
3 0.0 d f
4 4.0 e 0
5 5.0 f 0
In [67]: data.label
Out[67]:
0 c
1 d
2 NaN
3 f
4 NaN
5 NaN
Name: col2, dtype: object
In [68]: data['label']
Out[68]:
0 c
1 d
2 e
3 f
4 0
5 0
Name: label, dtype: object
Why data.label and data['label'] showing different results?
python pandas dataframe
This question already has an answer here:
In pandas, what's the difference between df['column'] and df.column?
1 answer
In [66]: data
Out[66]:
col1 col2 label
0 1.0 a c
1 2.0 b d
2 3.0 c e
3 0.0 d f
4 4.0 e 0
5 5.0 f 0
In [67]: data.label
Out[67]:
0 c
1 d
2 NaN
3 f
4 NaN
5 NaN
Name: col2, dtype: object
In [68]: data['label']
Out[68]:
0 c
1 d
2 e
3 f
4 0
5 0
Name: label, dtype: object
Why data.label and data['label'] showing different results?
This question already has an answer here:
In pandas, what's the difference between df['column'] and df.column?
1 answer
python pandas dataframe
python pandas dataframe
asked Nov 16 at 3:00
Sanjay Chouhan
1
1
marked as duplicate by Mohamed Thasin ah, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 at 20:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mohamed Thasin ah, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 at 20:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The difference between these two is related to assignment. with data.label
you cannot assign the values to column.
data.label is to access the attributes and data["label"] is to assign the values.
Also if you have spaces in your column name, for example df['label name']
, while using data.label name
will through an error.
For more information see this Answer link
add a comment |
The big difference I've noticed is assignment.
import random
import pandas as pd
s = "SummerCrime|WinterCrime".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)
df.FallCrime = [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)]
Gives: UserWarning: Pandas doesn't allow columns to be created via a new attribute name
However, there are also docs associated with this: https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
Which has the following warnings which may be related to your problem:
- You can use this access only if the index element is a valid Python
identifier, e.g. s.1 is not allowed.
- The attribute will not be available if it
conflicts with an existing method name, e.g. s.min is not allowed.
- Similarly, the attribute will not be available if it conflicts with
any of the following list: index, major_axis, minor_axis, items. In
any of these cases, standard indexing will still work, e.g. s['1'],
s['min'], and s['index'] will access the corresponding element or
column.
They go on to say:
You can use attribute access to modify an existing element of a Series or column of a
DataFrame, but be careful; if you try to use attribute access to create a new column,
it creates a new attribute rather than a new column.
**In 0.21.0 and later, this will raise a UserWarning**
(So it's possible you did this without realizing)
You should blockquote quotes from an external source, e.g. official documentation. And probably put your answer on the marked duplicate instead of here.
– jpp
Nov 17 at 20:27
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The difference between these two is related to assignment. with data.label
you cannot assign the values to column.
data.label is to access the attributes and data["label"] is to assign the values.
Also if you have spaces in your column name, for example df['label name']
, while using data.label name
will through an error.
For more information see this Answer link
add a comment |
The difference between these two is related to assignment. with data.label
you cannot assign the values to column.
data.label is to access the attributes and data["label"] is to assign the values.
Also if you have spaces in your column name, for example df['label name']
, while using data.label name
will through an error.
For more information see this Answer link
add a comment |
The difference between these two is related to assignment. with data.label
you cannot assign the values to column.
data.label is to access the attributes and data["label"] is to assign the values.
Also if you have spaces in your column name, for example df['label name']
, while using data.label name
will through an error.
For more information see this Answer link
The difference between these two is related to assignment. with data.label
you cannot assign the values to column.
data.label is to access the attributes and data["label"] is to assign the values.
Also if you have spaces in your column name, for example df['label name']
, while using data.label name
will through an error.
For more information see this Answer link
answered Nov 16 at 3:34
Chandila07
439212
439212
add a comment |
add a comment |
The big difference I've noticed is assignment.
import random
import pandas as pd
s = "SummerCrime|WinterCrime".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)
df.FallCrime = [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)]
Gives: UserWarning: Pandas doesn't allow columns to be created via a new attribute name
However, there are also docs associated with this: https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
Which has the following warnings which may be related to your problem:
- You can use this access only if the index element is a valid Python
identifier, e.g. s.1 is not allowed.
- The attribute will not be available if it
conflicts with an existing method name, e.g. s.min is not allowed.
- Similarly, the attribute will not be available if it conflicts with
any of the following list: index, major_axis, minor_axis, items. In
any of these cases, standard indexing will still work, e.g. s['1'],
s['min'], and s['index'] will access the corresponding element or
column.
They go on to say:
You can use attribute access to modify an existing element of a Series or column of a
DataFrame, but be careful; if you try to use attribute access to create a new column,
it creates a new attribute rather than a new column.
**In 0.21.0 and later, this will raise a UserWarning**
(So it's possible you did this without realizing)
You should blockquote quotes from an external source, e.g. official documentation. And probably put your answer on the marked duplicate instead of here.
– jpp
Nov 17 at 20:27
add a comment |
The big difference I've noticed is assignment.
import random
import pandas as pd
s = "SummerCrime|WinterCrime".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)
df.FallCrime = [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)]
Gives: UserWarning: Pandas doesn't allow columns to be created via a new attribute name
However, there are also docs associated with this: https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
Which has the following warnings which may be related to your problem:
- You can use this access only if the index element is a valid Python
identifier, e.g. s.1 is not allowed.
- The attribute will not be available if it
conflicts with an existing method name, e.g. s.min is not allowed.
- Similarly, the attribute will not be available if it conflicts with
any of the following list: index, major_axis, minor_axis, items. In
any of these cases, standard indexing will still work, e.g. s['1'],
s['min'], and s['index'] will access the corresponding element or
column.
They go on to say:
You can use attribute access to modify an existing element of a Series or column of a
DataFrame, but be careful; if you try to use attribute access to create a new column,
it creates a new attribute rather than a new column.
**In 0.21.0 and later, this will raise a UserWarning**
(So it's possible you did this without realizing)
You should blockquote quotes from an external source, e.g. official documentation. And probably put your answer on the marked duplicate instead of here.
– jpp
Nov 17 at 20:27
add a comment |
The big difference I've noticed is assignment.
import random
import pandas as pd
s = "SummerCrime|WinterCrime".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)
df.FallCrime = [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)]
Gives: UserWarning: Pandas doesn't allow columns to be created via a new attribute name
However, there are also docs associated with this: https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
Which has the following warnings which may be related to your problem:
- You can use this access only if the index element is a valid Python
identifier, e.g. s.1 is not allowed.
- The attribute will not be available if it
conflicts with an existing method name, e.g. s.min is not allowed.
- Similarly, the attribute will not be available if it conflicts with
any of the following list: index, major_axis, minor_axis, items. In
any of these cases, standard indexing will still work, e.g. s['1'],
s['min'], and s['index'] will access the corresponding element or
column.
They go on to say:
You can use attribute access to modify an existing element of a Series or column of a
DataFrame, but be careful; if you try to use attribute access to create a new column,
it creates a new attribute rather than a new column.
**In 0.21.0 and later, this will raise a UserWarning**
(So it's possible you did this without realizing)
The big difference I've noticed is assignment.
import random
import pandas as pd
s = "SummerCrime|WinterCrime".split("|")
j = {x: [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)] for x in s}
df = pd.DataFrame(j)
df.FallCrime = [random.choice(["ASB", "Violence", "Theft", "Public Order", "Drugs"]) for j in range(300)]
Gives: UserWarning: Pandas doesn't allow columns to be created via a new attribute name
However, there are also docs associated with this: https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
Which has the following warnings which may be related to your problem:
- You can use this access only if the index element is a valid Python
identifier, e.g. s.1 is not allowed.
- The attribute will not be available if it
conflicts with an existing method name, e.g. s.min is not allowed.
- Similarly, the attribute will not be available if it conflicts with
any of the following list: index, major_axis, minor_axis, items. In
any of these cases, standard indexing will still work, e.g. s['1'],
s['min'], and s['index'] will access the corresponding element or
column.
They go on to say:
You can use attribute access to modify an existing element of a Series or column of a
DataFrame, but be careful; if you try to use attribute access to create a new column,
it creates a new attribute rather than a new column.
**In 0.21.0 and later, this will raise a UserWarning**
(So it's possible you did this without realizing)
edited Nov 17 at 20:35
answered Nov 16 at 3:13
Charles Landau
1,8771215
1,8771215
You should blockquote quotes from an external source, e.g. official documentation. And probably put your answer on the marked duplicate instead of here.
– jpp
Nov 17 at 20:27
add a comment |
You should blockquote quotes from an external source, e.g. official documentation. And probably put your answer on the marked duplicate instead of here.
– jpp
Nov 17 at 20:27
You should blockquote quotes from an external source, e.g. official documentation. And probably put your answer on the marked duplicate instead of here.
– jpp
Nov 17 at 20:27
You should blockquote quotes from an external source, e.g. official documentation. And probably put your answer on the marked duplicate instead of here.
– jpp
Nov 17 at 20:27
add a comment |