Multiply by a float in pandas -> numbers with comma disappearing [duplicate] - python

This question already has answers here:
TypeError: can't multiply sequence by non-int of type 'float' (python 2.7)
(1 answer)
Finding non-numeric rows in dataframe in pandas?
(7 answers)
Change column type in pandas
(16 answers)
Closed 4 years ago.
Im having issue applying a currency rate in pandas.
Some numbers are being converted as 'nan' whenever they contains a comma, eg: 1,789 will be considered as nan.
I started with that code :
import pandas as pd
usd_rate = 0.77
salary = pd.read_csv("salary.csv")
#create revenue clean (convert usd to gbp)
salary['revenue_gbp'] = salary.usd_revenue * usd_rate
So I was getting that error :
TypeError: can't multiply sequence by non-int of type 'float'
I've read you can't multiply the column by a float. So I converted my column to numeric :
salary.net_revenue = pd.to_numeric(salary.usd_revenue, errors='coerce')
salary['revenue_gbp'] = salary.usd_revenue * usd_rate
Now I don't have any errors, yet when I looked at my file , all of the number above 999.99 - so the ones containing a comma - are put under 'nan'...
I thought it could be translate issue .. but I'm getting confused here..
any ideas ?
Thanks a lot

usd_revenue is probably not already a numeric type. Try this:
salary['usd_revenue'] = salary['usd_revenue'].map(float)
before your actual line:
salary['revenue_gbp'] = salary.usd_revenue * usd_rate

Related

How can I drop rows based on given conditions in pandas and how to force converting data types? [duplicate]

This question already has answers here:
Change column type in pandas
(16 answers)
Closed 4 months ago.
I want to drop rows that don't verify a condition, I tried the code below, but it's not working
sample.drop(sample.loc[(sample.service == 'ftp') & (sample.is_ftp_login.isna())].index, inplace=False)
I also tried a loop, with condition isna() and with ' ', but it didn't work
for index, row in sample.iterrows():
if row['service'] == 'ftp' and row['is_ftp_login'].isna():
sample.drop([index])
I also want to change types from object to int and from float to int, (I tried both lines) it returns cannot convert to Class int.
sample['ct_ftp_cmd']=int(sample['ct_ftp_cmd'])
sample['ct_ftp_cmd']=str(int(sample['ct_ftp_cmd']))
Do you guys have any idea how to solve this, Thanks.
I
Thank you, I solved the problem.
I was able to convert the string type to numeric using this:
sample['ct_ftp_cmd']= pd.to_numeric(sample['ct_ftp_cmd'])
and I dropped rows based on a given condition using this:
sample = sample.drop(sample[(sample.service == 'ftp') & (sample.is_ftp_login.isna())].index)

Convert objects to numeric values [duplicate]

This question already has answers here:
Change Pandas String Column with commas into Float
(2 answers)
Closed 6 months ago.
I have a CSV file and it has a column full of numbers. These numbers can be formatted as 45.11 , 1,234.33, 122.33, 10,222.22 etc.
Right now they are showing up as objects in my data frame, and i need to convert them to numeric. I have tried:
df['Value'].astype(str).astype(float)
But am getting errors like this:
ValueError: could not convert string to float: '1,054.43'
Does anyone know how to solve this for the weirdly formatted numbers?
this should make the job
vals={'Value': ["45.11" , "1,234.33", "122.33", "10,222.22"]}
df = pd.DataFrame(vals)
df.Value = df.Value.apply(lambda x: x.replace(",", "")).astype(float)
print(df.Value)
output
0 45.11
1 1234.33
2 122.33
3 10222.22
Name: Value, dtype: float64

Join 2 conditions using & operator [duplicate]

This question already has answers here:
Pandas: Filtering multiple conditions
(4 answers)
Closed 10 months ago.
i have 2 queries in pandas and need to join them together.
b.loc[b['Speed']=='100.0']
b.loc[b['Month']=='2022-01']
I need to join them using & but getting error of unsupported operand type.
You are comparing your data having different datatype with comparison value of str, while it should be float 64 and period M respectively as you have mentioned in your comment.
Try to match your comparison with correct data type. try this:
b.loc[(b['Speed'] == 100.0) & (b['Month'] == pd.Period('2022-01'))]

Having problems converting strings to floats in pandas data frame [duplicate]

This question already has answers here:
Convert number strings with commas in pandas DataFrame to float
(4 answers)
Closed 4 years ago.
Trying to make a scatter plot with a pandas dataframe, but "ValueError: x and y must be the same size" kept popping up. Looks like Slaughter Steers data column are strings instead of floats so try to convert it, but ValueError: could not convert string to float: '1,062.6' happens. Tried to replace ' with a space still same error.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
#Read in Data set date as index
cattle_price = pd.read_csv('C:/Users/SkyLH/Documents/CattleForcast Model/Slaughter Cattle Monthly Data.csv', index_col = 'DATE')
cattle_slaughter = pd.read_csv('C:/Users/SkyLH/Documents/Cattle Forcast Model/SlaughterCountsFull - Sheet1.csv', index_col = 'Date')
cattle_price.index = pd.to_datetime(cattle_price.index)
cattle_price.index.names = ['Date',]
cattle_slaughter.replace("'"," ")
cattle_slaughter.astype(float)
cattle_df = cattle_price.join(cattle_slaughter, how = 'inner')
print(cattle_df)
plt.scatter(cattle_df, y = 'Price')
plt.show()
Price Slaughter Steers
Date
1955-01-01 34.899999 983.8
1955-02-01 35.999998 847.9
1955-03-01 34.600001 1,062.6
1955-04-01 35.800002 1,000.9
1955-05-01 33.100002 1,090.1
Believe the commas (thousands separators) are preventing the conversion. This question has possible solutions that may help you:
How do I use Python to convert a string to a number if it has commas in it as thousands separators?

Pandas - ValueError: Error parsing datetime string "17-Jan-23" at position 3 [duplicate]

This question already has an answer here:
Error parsing datetime string "09-11-2017 00:02:00" at position 8
(1 answer)
Closed 4 years ago.
I have the following code where I am reading date column:
data = pd.DataFrame(array, columns=names)
data[['D_DATE']] = data[['D_DATE']].astype('datetime64')
But this is giving me error:
ValueError: Error parsing datetime string "17-Jan-23" at position 3
Can someone help how can I resolve this.
Try this:
data['D_DATE'] = pd.to_datetime(data['D_DATE'])
Indexing a single column with double brackets (df[['D_DATE']]) returns a DataFrame with one column named 'D_DATE'. Indexing with a single set of brackets (df['D_DATE']) returns a Series named 'D_DATE'. To create a new column in a DataFrame using the form df[new_col], use single brackets.

Categories