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;








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









share|improve this question






















  • @jazreal plz check this

    – nick
    Mar 26 at 10:00

















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









share|improve this question






















  • @jazreal plz check this

    – nick
    Mar 26 at 10:00













0












0








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • @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












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
);



);













draft saved

draft discarded


















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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?