This question already has answers here:
ultimate short custom number formatting - K, M, B, T, etc., Q, D, Googol
(3 answers)
Closed 1 year ago.
I want to make a number format in Google Sheets that turns large numbers into their abbreviated form. Example: "1 200" -> "1.2k", "1 500 000 000 000 000" (one point five quadrillions) -> "1.5Qa". I have absolutely no idea on how would that look.
Thanks in advance.
this should cover your needs:
=ARRAYFORMULA(IF(A:A<10^3, A:A,
IF(1*A:A<10^6, TEXT(A:A/10^3, "#.0\k"),
IF(1*A:A<10^9, TEXT(A:A/10^6, "#.0\M"),
IF(1*A:A<10^12, TEXT(A:A/10^9, "#.0\B"),
IF(1*A:A<10^15, TEXT(A:A/10^12, "#.0\T"),
IF(1*A:A<10^18, TEXT(A:A/10^15, "#.0\Q\a"),
IF(1*A:A<10^21, TEXT(A:A/10^18, "#.0\Q\i"),
IF(1*A:A<10^24, TEXT(A:A/10^21, "#.0\S\x"),
IF(1*A:A<10^27, TEXT(A:A/10^24, "#.0\S\p"),
IF(1*A:A<10^30, TEXT(A:A/10^27, "#.0\O"),
IF(1*A:A<10^33, TEXT(A:A/10^30, "#.0\N"),
IF(1*A:A<10^36, TEXT(A:A/10^33, "#.0\D"),
IF(1*A:A<10^39, TEXT(A:A/10^36, "#.0\U"),
IF(1*A:A<10^42, TEXT(A:A/10^39, "#.0\D\d"),
IF(1*A:A<10^45, TEXT(A:A/10^42, "#.0\T\d"),
IF(1*A:A<10^48, TEXT(A:A/10^45, "#.0\Q\a\d"),
IF(1*A:A<10^51, TEXT(A:A/10^48, "#.0\Q\u\d"),
IF(1*A:A<10^54, TEXT(A:A/10^51, "#.0\S\x\d"),
IF(1*A:A<10^57, TEXT(A:A/10^54, "#.0\S\p\d"),
IF(1*A:A<10^60, TEXT(A:A/10^57, "#.0\O\d"),
IF(1*A:A<10^63, TEXT(A:A/10^60, "#.0\N\d"),
IF(1*A:A<10^66, TEXT(A:A/10^63, "#.0\V"),
IF(1*A:A<10^69, TEXT(A:A/10^66, "#.0\C"), ))))))))))))))))))))))))
Use a custom number format
Select the range of cells you want to convert
Go to Format -> Number -> More Formats -> Custom number format
Paste into the input field [>999999]#,,"M";#,"K"
Click on Apply - Done
I do not think it is possible to configure more than two formats of a cell to adapt dynamically according to the number inside it without some scripting. That would be nice as it would preserved the number type.
But if there is no need to preserve the number type and string is acceptable, then strings could be generated like this using TEXT function and dynamically setting format for the number based on a reference:
=INDEX(
TEXT(
E2:E24,
"0.0"
& IFNA(
REPT(",", (VLOOKUP(INT(LOG10(E2:E24)), $C$2:$C$8, 1, TRUE)) / 3)
& "\" & VLOOKUP(INT(LOG10(E2:E24)), {$C$2:$C$8, $A$2:$A$8}, 2, TRUE)
)
)
)
On the left you can see a reference columns where I used symbols from wiki.
This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Closed 6 years ago.
I am using python to learn about data science. Everything is fine but recently I found below code in a book. I can't understand for what purpose '_' is being used.
def raw_majority_vote(labels):
votes = Counter(labels)
winner, _ = votes.most_common(1)[0]
return winner
In this piece of code you posted, the _ is a variable name.
You can assign values to _.
I.e.:
>>> _ = "test"
>>> print _
Output:
test
If you take a look at Counter.most_common() docs, you'll see this message:
Return a list of the n most common elements and their counts from the
most common to the least. If n is omitted or None, most_common()
returns all elements in the counter. Elements with equal counts are
ordered arbitrarily:
>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
So, in your code, winner, _ = votes.most_common(1)[0]
The variable winner gets the first value of the first tuple contained in this most_common list.
And the variable, _, gets the second value of the first tuple in this list.
In this case:
winner = 'a'
_ = 5
It's a throwaway variable. Whatever votes.most_common(1)[0] is can be unpacked to two values and the writer of that script is only interested in the first value.
Usually it is used when you don't care about returned variable and you want to discard it but still prevent any ValueErrors.
I am trying to just do a basic INSERT operation to a PostgreSQL database through Python via the Psycopg2 module. I have read a great many of the questions already posted regarding this subject as well as the documentation but I seem to have done something uniquely wrong and none of the fixes seem to work for my code.
#API CALL + JSON decoding here
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers
)
The loop creates a list of strings that looks like this when printed:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
I am just trying to have the code INSERT these strings as 1 row each into the table.
The error specified when running is:
TypeError: not all arguments converted during string formatting
I tried changing the INSERT to:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", (activeUsers,) )
But that seems like it's merely treating the entire list as a single string as it yields:
psycopg2.DataError: value too long for type character varying(30)
What am I missing?
First in the code you pasted:
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
Is not the right way to accomplish what you are trying to do.
first list is a reserved word in python and you shouldn't use it as a variable name. I am assuming you meant ulist.
if you really need access to the index of an item in python you can use enumerate:
for x, item in enumerate(ulist):
but, the best way to do what you are trying to do is something like
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append(str(item['name']))
Your first try was:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
Your second attempt was:
(['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo'], )
What I think you want is:
[['b2ong'], ['dune'], ['drble'], ['drars'], ['feman'], ['got'], ['urbo']]
You could get this many ways:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", [ [a] for a in activeUsers] )
or event better:
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append([str(item['name'])])
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers)
I am using Stata 14. I have US states and corresponding regions as integer.
I want create a string variable that represents the region for each observation.
Currently my code is
gen div_name = "A"
replace div_name = "New England" if div_no == 1
replace div_name = "Middle Atlantic" if div_no == 2
.
.
replace div_name = "Pacific" if div_no == 9
..so it is a really long code.
I was wondering if there is a shorter way to do this where I can automate assigning values rather than manually hard coding them.
You can define value labels in one line with label define and then use decode to create the string variable. See the help for those commands.
If the correspondence was defined in a separate dataset you could use merge. See e.g. this FAQ
There can't be a short-cut here other than typing all the names at some point or exploiting the fact that someone else typed them earlier into a file.
With nine or so labels, typing them yourself is quickest.
Note that you type one statement more than you need, even doing it the long way, as you could start
gen div_name = "New England" if div_no == 1
Question
The goal is to find a simple way for converting a set of data to a string.
Maybe I am too newby but I found nothing about conversion from a set to string. A similar question (Numpy converting array from float to strings) did not help me a lot.
Example
The code I wrote seems definitely not ideal:
DataSet = {(1,4): 272.3,
(2,4): 274.74}
print(', '.join(np.array(DataSet).astype('str'))))
Personal goal
In the end I want to create a string like:
DataSet = {(1,4): 272.3,
(2,4): 274.74}
version = 2.7
print(''.join(np.array(['The data in {',
', '.join(np.array(DataSet).astype('str'))),
'} is calculated with python%3.1f.' % version]))
The output should look like (it would be nice but not necessary to implement some fixed float precision):
'The data in {272.3, 274.7} is calculated with python2.7.'
DataSet is a Python dictionary and thus DataSet.values() returns the list of its values.
The string you need can be generated by ', '.join(map(str, DataSet.values())) or ', '.join(str(v) for v in DataSet.values()).