Why can't you replace integers with lists using `replace` method - pandas
up vote
5
down vote
favorite
So let's say i have a pandas data-frame as below:
df=pd.DataFrame({'a':[1,2,3,0]})
So my goal is to replace 0
value with (empty list) in this data-frame, but i did:
print(df.replace(0,))
But it gives me an error:
TypeError: Invalid "to_replace" type: 'int'
I tried everything that's possible i.e:
df[df==0]=
etc...
But nothing works.
Desired output (in case for confusion):
a
0 1
1 2
2 3
3
python pandas dataframe replace types
|
show 4 more comments
up vote
5
down vote
favorite
So let's say i have a pandas data-frame as below:
df=pd.DataFrame({'a':[1,2,3,0]})
So my goal is to replace 0
value with (empty list) in this data-frame, but i did:
print(df.replace(0,))
But it gives me an error:
TypeError: Invalid "to_replace" type: 'int'
I tried everything that's possible i.e:
df[df==0]=
etc...
But nothing works.
Desired output (in case for confusion):
a
0 1
1 2
2 3
3
python pandas dataframe replace types
1
It does the trick withprint(df.replace(0,""))
– pygo
2 days ago
Amm... i want a real list that is append-able...
– U9-Forward
2 days ago
1
becausedf.a
is of typeint
. You need it to be of typeobject
. e.g. try initialize df withdf=pd.DataFrame({'a':[1,2,3,]})
and compare thendf.a.dtype
– SpghttCd
2 days ago
@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago
1
@U9-Forward The method suggested by SpghttCd withloc
works checkdf=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a']))
.
– Sandeep Kadapa
2 days ago
|
show 4 more comments
up vote
5
down vote
favorite
up vote
5
down vote
favorite
So let's say i have a pandas data-frame as below:
df=pd.DataFrame({'a':[1,2,3,0]})
So my goal is to replace 0
value with (empty list) in this data-frame, but i did:
print(df.replace(0,))
But it gives me an error:
TypeError: Invalid "to_replace" type: 'int'
I tried everything that's possible i.e:
df[df==0]=
etc...
But nothing works.
Desired output (in case for confusion):
a
0 1
1 2
2 3
3
python pandas dataframe replace types
So let's say i have a pandas data-frame as below:
df=pd.DataFrame({'a':[1,2,3,0]})
So my goal is to replace 0
value with (empty list) in this data-frame, but i did:
print(df.replace(0,))
But it gives me an error:
TypeError: Invalid "to_replace" type: 'int'
I tried everything that's possible i.e:
df[df==0]=
etc...
But nothing works.
Desired output (in case for confusion):
a
0 1
1 2
2 3
3
python pandas dataframe replace types
python pandas dataframe replace types
edited 2 days ago
asked 2 days ago
U9-Forward
9,9682834
9,9682834
1
It does the trick withprint(df.replace(0,""))
– pygo
2 days ago
Amm... i want a real list that is append-able...
– U9-Forward
2 days ago
1
becausedf.a
is of typeint
. You need it to be of typeobject
. e.g. try initialize df withdf=pd.DataFrame({'a':[1,2,3,]})
and compare thendf.a.dtype
– SpghttCd
2 days ago
@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago
1
@U9-Forward The method suggested by SpghttCd withloc
works checkdf=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a']))
.
– Sandeep Kadapa
2 days ago
|
show 4 more comments
1
It does the trick withprint(df.replace(0,""))
– pygo
2 days ago
Amm... i want a real list that is append-able...
– U9-Forward
2 days ago
1
becausedf.a
is of typeint
. You need it to be of typeobject
. e.g. try initialize df withdf=pd.DataFrame({'a':[1,2,3,]})
and compare thendf.a.dtype
– SpghttCd
2 days ago
@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago
1
@U9-Forward The method suggested by SpghttCd withloc
works checkdf=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a']))
.
– Sandeep Kadapa
2 days ago
1
1
It does the trick with
print(df.replace(0,""))
– pygo
2 days ago
It does the trick with
print(df.replace(0,""))
– pygo
2 days ago
Amm... i want a real list that is append-able...
– U9-Forward
2 days ago
Amm... i want a real list that is append-able...
– U9-Forward
2 days ago
1
1
because
df.a
is of type int
. You need it to be of type object
. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]})
and compare then df.a.dtype
– SpghttCd
2 days ago
because
df.a
is of type int
. You need it to be of type object
. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]})
and compare then df.a.dtype
– SpghttCd
2 days ago
@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago
@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago
1
1
@U9-Forward The method suggested by SpghttCd with
loc
works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a']))
.– Sandeep Kadapa
2 days ago
@U9-Forward The method suggested by SpghttCd with
loc
works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a']))
.– Sandeep Kadapa
2 days ago
|
show 4 more comments
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:
df['a'] = [ if x == 0 else x for x in df.a]
print (df)
a
0 1
1 2
2 3
3
And replace all values in all columns:
df = df.applymap(lambda x: if x == 0 else x)
print (df)
a
0 1
1 2
2 3
3
1
Note that[] * len(df)
may not work iflen(df) > 1
. It will create a list containing the same instance of empty list rather thanlen(df)
unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
– Lie Ryan
2 days ago
1
Thanks so much, i appreciate it, came back and accepted this!!!
– U9-Forward
yesterday
1
@U9-Forward - Thank you very much!
– jezrael
yesterday
add a comment |
up vote
1
down vote
There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
This creates n
empty list based on the number of items that matches the criteria (df == 0
)
The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.
df = df.astype(object)
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
add a comment |
up vote
-1
down vote
This works for me:
df=pd.DataFrame({'a':[1,8,3,0]})
print(df)
a
0 1
1 8
2 3
3 0
df['a'] = df['a'].apply(lambda d: d if d!=0 else )
print(df)
a
0 1
1 8
2 3
3
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:
df['a'] = [ if x == 0 else x for x in df.a]
print (df)
a
0 1
1 2
2 3
3
And replace all values in all columns:
df = df.applymap(lambda x: if x == 0 else x)
print (df)
a
0 1
1 2
2 3
3
1
Note that[] * len(df)
may not work iflen(df) > 1
. It will create a list containing the same instance of empty list rather thanlen(df)
unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
– Lie Ryan
2 days ago
1
Thanks so much, i appreciate it, came back and accepted this!!!
– U9-Forward
yesterday
1
@U9-Forward - Thank you very much!
– jezrael
yesterday
add a comment |
up vote
3
down vote
accepted
It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:
df['a'] = [ if x == 0 else x for x in df.a]
print (df)
a
0 1
1 2
2 3
3
And replace all values in all columns:
df = df.applymap(lambda x: if x == 0 else x)
print (df)
a
0 1
1 2
2 3
3
1
Note that[] * len(df)
may not work iflen(df) > 1
. It will create a list containing the same instance of empty list rather thanlen(df)
unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
– Lie Ryan
2 days ago
1
Thanks so much, i appreciate it, came back and accepted this!!!
– U9-Forward
yesterday
1
@U9-Forward - Thank you very much!
– jezrael
yesterday
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:
df['a'] = [ if x == 0 else x for x in df.a]
print (df)
a
0 1
1 2
2 3
3
And replace all values in all columns:
df = df.applymap(lambda x: if x == 0 else x)
print (df)
a
0 1
1 2
2 3
3
It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:
df['a'] = [ if x == 0 else x for x in df.a]
print (df)
a
0 1
1 2
2 3
3
And replace all values in all columns:
df = df.applymap(lambda x: if x == 0 else x)
print (df)
a
0 1
1 2
2 3
3
edited 2 days ago
answered 2 days ago
jezrael
308k20245319
308k20245319
1
Note that[] * len(df)
may not work iflen(df) > 1
. It will create a list containing the same instance of empty list rather thanlen(df)
unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
– Lie Ryan
2 days ago
1
Thanks so much, i appreciate it, came back and accepted this!!!
– U9-Forward
yesterday
1
@U9-Forward - Thank you very much!
– jezrael
yesterday
add a comment |
1
Note that[] * len(df)
may not work iflen(df) > 1
. It will create a list containing the same instance of empty list rather thanlen(df)
unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
– Lie Ryan
2 days ago
1
Thanks so much, i appreciate it, came back and accepted this!!!
– U9-Forward
yesterday
1
@U9-Forward - Thank you very much!
– jezrael
yesterday
1
1
Note that
[] * len(df)
may not work if len(df) > 1
. It will create a list containing the same instance of empty list rather than len(df)
unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.– Lie Ryan
2 days ago
Note that
[] * len(df)
may not work if len(df) > 1
. It will create a list containing the same instance of empty list rather than len(df)
unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.– Lie Ryan
2 days ago
1
1
Thanks so much, i appreciate it, came back and accepted this!!!
– U9-Forward
yesterday
Thanks so much, i appreciate it, came back and accepted this!!!
– U9-Forward
yesterday
1
1
@U9-Forward - Thank you very much!
– jezrael
yesterday
@U9-Forward - Thank you very much!
– jezrael
yesterday
add a comment |
up vote
1
down vote
There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
This creates n
empty list based on the number of items that matches the criteria (df == 0
)
The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.
df = df.astype(object)
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
add a comment |
up vote
1
down vote
There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
This creates n
empty list based on the number of items that matches the criteria (df == 0
)
The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.
df = df.astype(object)
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
add a comment |
up vote
1
down vote
up vote
1
down vote
There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
This creates n
empty list based on the number of items that matches the criteria (df == 0
)
The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.
df = df.astype(object)
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
This creates n
empty list based on the number of items that matches the criteria (df == 0
)
The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.
df = df.astype(object)
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]
answered 2 days ago
Lie Ryan
43.8k868121
43.8k868121
add a comment |
add a comment |
up vote
-1
down vote
This works for me:
df=pd.DataFrame({'a':[1,8,3,0]})
print(df)
a
0 1
1 8
2 3
3 0
df['a'] = df['a'].apply(lambda d: d if d!=0 else )
print(df)
a
0 1
1 8
2 3
3
add a comment |
up vote
-1
down vote
This works for me:
df=pd.DataFrame({'a':[1,8,3,0]})
print(df)
a
0 1
1 8
2 3
3 0
df['a'] = df['a'].apply(lambda d: d if d!=0 else )
print(df)
a
0 1
1 8
2 3
3
add a comment |
up vote
-1
down vote
up vote
-1
down vote
This works for me:
df=pd.DataFrame({'a':[1,8,3,0]})
print(df)
a
0 1
1 8
2 3
3 0
df['a'] = df['a'].apply(lambda d: d if d!=0 else )
print(df)
a
0 1
1 8
2 3
3
This works for me:
df=pd.DataFrame({'a':[1,8,3,0]})
print(df)
a
0 1
1 8
2 3
3 0
df['a'] = df['a'].apply(lambda d: d if d!=0 else )
print(df)
a
0 1
1 8
2 3
3
answered 2 days ago
nixon
83116
83116
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410163%2fwhy-cant-you-replace-integers-with-lists-using-replace-method-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
It does the trick with
print(df.replace(0,""))
– pygo
2 days ago
Amm... i want a real list that is append-able...
– U9-Forward
2 days ago
1
because
df.a
is of typeint
. You need it to be of typeobject
. e.g. try initialize df withdf=pd.DataFrame({'a':[1,2,3,]})
and compare thendf.a.dtype
– SpghttCd
2 days ago
@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago
1
@U9-Forward The method suggested by SpghttCd with
loc
works checkdf=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a']))
.– Sandeep Kadapa
2 days ago