How to change values in a column from object type to float. for Example, "'€220 M" to 220,000,000? [duplicate] - pandas

This question already has answers here:
Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe
(6 answers)
Closed 3 years ago.
I have a column with data as:
1 77M
2 118.5M
3 72M
4 102M
5 93M
6 67M
I need to change this to its numerical value as:
77,000,000
and so on.
I have tried different ways but could not come up with a definite solution.

okay this should work
(df[1].str.replace('M','').astype(float) * 1000000).astype(int).astype(str).apply(lambda x : x[:-6]+','+x[-6:-3]+','+x[-3:])
Output
0 77,000,000
1 118,500,000
2 72,000,000
3 102,000,000
4 93,000,000
5 67,000,000
Name: 1, dtype: object

Related

SQL syntax - division and ceiling functions [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 2 years ago.
I am trying to calculate how many packs of sweats a kid needs based on the amount of individual sweets required. I am trying to figure out the SQL syntax to do this.
I have used ceiling with divide but still not working
select CEILING(NoOfSweets / SweetsPerPack) AS NoOfPacks
Scenarios:
NoOfSweets SweetsPerPack RequiredOutCome NoOfPacks
--------------------------------------------------------
10 10 1 1
5 10 1 0
20 10 2 2
8 10 1 0
7 5 2 1
If the values are integers, then SQL Server does integer division. So, 1/2 is 0 rather than 0.5. I find that the simplest way to get a number with decimal points is to multiply by 1.0:
CEILING(NoOfSweets * 1.0 / SweetsPerPack)

Merging two dataframes on the same type column gives me wrong result

I have two dataframes, assume A and B, which have been created after reading the sheets of an Excel file and performing some basic functions. I need to merge right the two dataframes on a column named ID which has first been converted to astype(str) for both dataframes.
The ID column of the left Dataframe (A) is:
0 5815518813016
1 5835503994014
2 5835504934023
3 5845535359006
4 5865520960012
5 5865532845006
6 5875531550008
7 5885498289039
8 5885498289039_A2
9 5885498289039_A3
10 5885498289039_X2
11 5885498289039_X3
12 5885509768698
13 5885522349999
14 5895507791025
Name: ID, dtype: object
The ID column of the right Dataframe (B) is:
0 5835503994014
1 5845535359006
2 5835504934023
3 5815518813016
4 5885498289039_A1
5 5885498289039_A2
6 5885498289039_A3
7 5885498289039_X1
8 5885498289039_X2
9 5885498289039_X3
10 5885498289039
11 5865532845006
12 5875531550008
13 5865520960012
14 5885522349998
15 5895507791025
16 5885509768698
Name: ID, dtype: object
However, when I merge the two, the rest of the columns of the left (A) dataframe become "empty" (np.nan) except for the rows where the ID does not contain only numbers but letters too. This is the pd.merge() I do:
A_B=A.merge(B[['ID','col_B']], left_on='ID', right_on='ID', how='right')
Do you have any ideas what might be so wrong? Your input is valuable.
Try turning all values in both columns into strings:
A['ID'] = A['ID'].astype(str)
B['ID'] = B['ID'].astype(str)
Generally, when a merge like this doesn't work, I would try to debug by printing out the unique values in each column to check if anything pops out (usually dtype issues).

I wanted to display numbers as alphanumberics [duplicate]

This question already has answers here:
how to display number value in words
(4 answers)
Closed 6 years ago.
Suppose in table I have a number column as
1
2
3
4
I want to display those rows as
one
two
three
four
How can I do using SQL
You can use a technique from this blog which uses a hack with dates to get the text version of numeric fields. The blog post goes into much more detail, but in short, it converts the number to a Julian date which lets TO_CHAR use the format specifier sp (spelling out in text)
SELECT num, TO_CHAR(TO_DATE(num, 'J'), 'Jsp') num_as_text
FROM myTable
ORDER BY num;
# num num_as_text
# ----------------
# 1 One
# 2 Two
# 3 Three
# 4 Four
You could use the j --> jsp technique to spell the number. It's been a FAQ.
j = julian. take the number and pretend it is a julian date, convert
it into a date.
jsp = Take that date and spell the julian number it represents.
For example,
SQL> SELECT LEVEL,
2 to_char(to_date(LEVEL,'j'), 'jsp') num_spell
3 FROM dual
4 CONNECT BY LEVEL <= 10;
LEVEL NUM_SPELL
---------- ----------
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
10 rows selected.
SQL>
Assuming the schema is structured as you suggest like:
Table
1 2 3 4
value value value value
value value value value
value value value value
You can use AS to rename the column in a query:
SELECT 1 AS one
2 AS two
3 AS three
4 AS four
FROM table

Pivot 1 row and n columns to n rows and 2 columns [duplicate]

This question already has answers here:
SQL Server : Columns to Rows
(7 answers)
SQL Server dynamic PIVOT query?
(9 answers)
Closed 8 years ago.
Say I have a table with a number of columns that is unknown to me, but only a single row:
A B C D E F G...n
1 2 3 4 5 6 7...x
And I would like to pivot it into a table of LABEL:VALUE pairs:
LABEL VALUE
A 1
B 2
C 3
... ...
n x
This seems like it should be a regular pivot (I think...sorry, a bit of a n00b to SQL), but don't I need to know the names of the columns that I wish to pivot, preventing me from pivoting an unknown number of columns? Is there a simple way to accomplish this?

Convert String into digit in SQL [duplicate]

This question already has answers here:
Converting words to numbers in PHP
(6 answers)
Closed 8 years ago.
How to convert any string into digit in SQL without CASE and Decode function.
eg. THREE to 3
FOUR to 4
FIVE to 5
SIX to 6
Range is not decided.. can be vary upto N.
Well, I'm not sure whether this is what you need, but what about defining a table, say digits, like this:
digit: text | value: int
------------+-----------
one | 1
two | 2
three | 3
etc.
Then use a query, for example, like this one:
SELECT value FROM digits WHERE digit = 'FIVE'
Sure, it's pretty weird (to say the least), but nonetheless the use of CASE is avoided.