Python Replace multiple characters in a string in entire pandas dataframeHow can I selectively escape percent (%) in Python strings?How to sort a dataFrame in python pandas by two or more columns?How to display pandas DataFrame of floats using a format string for columns?How do I create test and train samples from one dataframe with pandas?Check if string is in a pandas dataframeSplit and Pivot a Data FrameMultiple characters replace in a string pythonPandas dataframe replace string in multiple columns by finding substringMultiple Criteria Pandas Dataframe - PythonReplace Substring of Specific Pandas Dataframe Column
Why should I cook the flour first when making bechamel sauce?
Teferi's Time Twist on creature with +1/+1 counter
Index Uniqueness Overhead
Doing research in academia and not liking competition
Can you perfectly wrap a cube with this blocky shape?
Using two linked programs, output ordinal numbers up to n
Can a pizza stone be fixed after soap has been used to clean it?
What systems of robust steganography are out there?
What is the technical explanation of the note "A♭" in a F7 chord in the key of C?
Is there an English equivalent for "Les carottes sont cuites", while keeping the vegetable reference?
Print all lines that don't have numbers, using sed
What are "full piece" and "half piece" in chess?
Adding a vertical line at the right end of the horizontal line in frac
If a player tries to persuade somebody, what should that creature roll not to be persuaded?
Was all the fuel expended in each stage of a Saturn V launch?
If I stood next to a piece of metal heated to a million degrees, but in a perfect vacuum, would I feel hot?
What is this called? A tube flange bearing threaded for threaded pushrod
Why do legislative committees exist?
Animal Shelter Management C++
In Adventurers League, is there any way for an 5th-level wizard to gain heavy armor proficiency?
Was Willow's first magic display (blazing arrow through arm) actual magic, and if not, what's the trick?
Source of story about the Vilna Gaon and immigration policy
Too many spies!
Why aren't globular clusters disk shaped
Python Replace multiple characters in a string in entire pandas dataframe
How can I selectively escape percent (%) in Python strings?How to sort a dataFrame in python pandas by two or more columns?How to display pandas DataFrame of floats using a format string for columns?How do I create test and train samples from one dataframe with pandas?Check if string is in a pandas dataframeSplit and Pivot a Data FrameMultiple characters replace in a string pythonPandas dataframe replace string in multiple columns by finding substringMultiple Criteria Pandas Dataframe - PythonReplace Substring of Specific Pandas Dataframe Column
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to replace multiple characters in a string of an entire dataframe with an underscore ('_')
Dataframe:
Col1 Col2 b
0 a b a b 3
1 b.1 b.1 4
2 a+b a+b 5
3 a - b a - b 6
4 a-b a-b 7
I need to replace
' ' ---> '_' (whitespace to underscore)
' - ' ---> '___' (three unserscores)
+,-,/,*,% ---> '_' (all the chars to underscores)
I am using python 2.7 and pandas 0.22.0
I have tried different ways to achieve this but values are not getting updated,
replacing column wise can be done but I need to replace for an entire data frame irrespective of the number of columns
df.replace('Col1': ' ': '_', '.': '_','-':'_', regex=True)
and
df1 = df['A'].str.replace([' ','-','+'], ['_','_','_'],regex=True)
Expected output
Col1 Col2 b
0 a_b a_b 3
1 b_1 b_1 4
2 a_b a_b 5
3 a___b a___b 6
4 a_b a_b 7
python-2.7
add a comment |
I am trying to replace multiple characters in a string of an entire dataframe with an underscore ('_')
Dataframe:
Col1 Col2 b
0 a b a b 3
1 b.1 b.1 4
2 a+b a+b 5
3 a - b a - b 6
4 a-b a-b 7
I need to replace
' ' ---> '_' (whitespace to underscore)
' - ' ---> '___' (three unserscores)
+,-,/,*,% ---> '_' (all the chars to underscores)
I am using python 2.7 and pandas 0.22.0
I have tried different ways to achieve this but values are not getting updated,
replacing column wise can be done but I need to replace for an entire data frame irrespective of the number of columns
df.replace('Col1': ' ': '_', '.': '_','-':'_', regex=True)
and
df1 = df['A'].str.replace([' ','-','+'], ['_','_','_'],regex=True)
Expected output
Col1 Col2 b
0 a_b a_b 3
1 b_1 b_1 4
2 a_b a_b 5
3 a___b a___b 6
4 a_b a_b 7
python-2.7
@jazreal plz check this
– nick
Mar 26 at 10:00
add a comment |
I am trying to replace multiple characters in a string of an entire dataframe with an underscore ('_')
Dataframe:
Col1 Col2 b
0 a b a b 3
1 b.1 b.1 4
2 a+b a+b 5
3 a - b a - b 6
4 a-b a-b 7
I need to replace
' ' ---> '_' (whitespace to underscore)
' - ' ---> '___' (three unserscores)
+,-,/,*,% ---> '_' (all the chars to underscores)
I am using python 2.7 and pandas 0.22.0
I have tried different ways to achieve this but values are not getting updated,
replacing column wise can be done but I need to replace for an entire data frame irrespective of the number of columns
df.replace('Col1': ' ': '_', '.': '_','-':'_', regex=True)
and
df1 = df['A'].str.replace([' ','-','+'], ['_','_','_'],regex=True)
Expected output
Col1 Col2 b
0 a_b a_b 3
1 b_1 b_1 4
2 a_b a_b 5
3 a___b a___b 6
4 a_b a_b 7
python-2.7
I am trying to replace multiple characters in a string of an entire dataframe with an underscore ('_')
Dataframe:
Col1 Col2 b
0 a b a b 3
1 b.1 b.1 4
2 a+b a+b 5
3 a - b a - b 6
4 a-b a-b 7
I need to replace
' ' ---> '_' (whitespace to underscore)
' - ' ---> '___' (three unserscores)
+,-,/,*,% ---> '_' (all the chars to underscores)
I am using python 2.7 and pandas 0.22.0
I have tried different ways to achieve this but values are not getting updated,
replacing column wise can be done but I need to replace for an entire data frame irrespective of the number of columns
df.replace('Col1': ' ': '_', '.': '_','-':'_', regex=True)
and
df1 = df['A'].str.replace([' ','-','+'], ['_','_','_'],regex=True)
Expected output
Col1 Col2 b
0 a_b a_b 3
1 b_1 b_1 4
2 a_b a_b 5
3 a___b a___b 6
4 a_b a_b 7
python-2.7
python-2.7
asked Mar 26 at 7:32
nicknick
441 silver badge10 bronze badges
441 silver badge10 bronze badges
@jazreal plz check this
– nick
Mar 26 at 10:00
add a comment |
@jazreal plz check this
– nick
Mar 26 at 10:00
@jazreal plz check this
– nick
Mar 26 at 10:00
@jazreal plz check this
– nick
Mar 26 at 10:00
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55351854%2fpython-replace-multiple-characters-in-a-string-in-entire-pandas-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55351854%2fpython-replace-multiple-characters-in-a-string-in-entire-pandas-dataframe%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
@jazreal plz check this
– nick
Mar 26 at 10:00