Convert columns to string in Pandas

One way to convert to string is to use astype: total_rows[‘ColumnID’] = total_rows[‘ColumnID’].astype(str) However, perhaps you are looking for the to_json function, which will convert keys to valid json (and therefore your keys to strings): In [11]: df = pd.DataFrame([[‘A’, 2], [‘A’, 4], [‘B’, 6]]) In [12]: df.to_json() Out[12]: ‘{“0”:{“0″:”A”,”1″:”A”,”2″:”B”},”1″:{“0″:2,”1″:4,”2”:6}}’ In [13]: df[0].to_json() Out[13]: ‘{“0″:”A”,”1″:”A”,”2″:”B”}’ … Read more

How can I remove a character from a string using JavaScript?

var mystring = “crt/r2002_2″; mystring = mystring.replace(‘/r’,”https://stackoverflow.com/”); will replace /r with / using String.prototype.replace. Alternatively you could use regex with a global flag (as suggested by Erik Reppen & Sagar Gala, below) to replace all occurrences with mystring = mystring.replace(/\/r/g, “https://stackoverflow.com/”); EDIT: Since everyone’s having so much fun here and user1293504 doesn’t seem to be … Read more

Different font size of strings in the same TextView

Use a Spannable String String s= “Hello Everyone”; SpannableString ss1= new SpannableString(s); ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color TextView tv= (TextView) findViewById(R.id.textview); tv.setText(ss1); Snap shot You can split string using space and add span to the string you require. String s= “Hello Everyone”; String[] each = s.split(” … Read more

Should I use Java’s String.format() if performance is important?

I took hhafez code and added a memory test: private static void test() { Runtime runtime = Runtime.getRuntime(); long memory; … memory = runtime.freeMemory(); // for loop code memory = memory-runtime.freeMemory(); I run this separately for each approach, the ‘+’ operator, String.format and StringBuilder (calling toString()), so the memory used will not be affected by … Read more

Fastest way to iterate over all the chars in a String

FIRST UPDATE: Before you try this ever in a production environment (not advised), read this first: http://www.javaspecialists.eu/archive/Issue237.html Starting from Java 9, the solution as described won’t work anymore, because now Java will store strings as byte[] by default. SECOND UPDATE: As of 2016-10-25, on my AMDx64 8core and source 1.8, there is no difference between … Read more

Why is String immutable in Java?

String is immutable for several reasons, here is a summary: Security: parameters are typically represented as String in network connections, database connection urls, usernames/passwords etc. If it were mutable, these parameters could be easily changed. Synchronization and concurrency: making String immutable automatically makes them thread safe thereby solving the synchronization issues. Caching: when compiler optimizes … Read more