Pandas: converting strings with "$" to float [duplicate] - python

This question already has answers here:
pandas - how to convert all columns from object to float type
(3 answers)
Closed 4 years ago.
I am a beginner trying to analyze a dataset of Congressional campaign funding sources but they are all string values with '$' in them. How can I quickly change every value into a numerical value?enter image description here

states_table[dollar_columns] = states_table[dollar_columns].replace('[\$,]', '', regex=True).astype(float)
Where dollar_columns is a list of the columns you want to convert. For instance:
dollar_columns = ['net_con', 'net_ope_exp']

Related

How can I type to see only one row in df.head() [duplicate]

This question already has answers here:
How to print a specific row of a pandas DataFrame?
(6 answers)
Closed 11 months ago.
For example, if I want to see only one sentence in the dataframe in row 21, how can I type in the head function?
df.head(20)? df.head(19:20)
you can access elements with their integer position df.iat[row,col]
or an integer row with df.iloc([row])

Trying to use transform in pandas but it is giving me some error [duplicate]

This question already has answers here:
Converting string column from DataFrame to float for .sum()
(4 answers)
Change column type in pandas
(16 answers)
Closed 1 year ago.
I a trying to get the sum of two numbers by using groupby and transform in pandas library but It is giving some garbage value, can someone guide me on how to solve this:
my data looks like this:
SKU Fees
45241 6.91
45241 6.91
55732 119.05
55732 137.98
I have tried using this code:
df['total_fees'] = df.groupby(['sku'])['Fees'].transform('sum')
what I am getting is this:
SKU Fees total_fees
45241 6.91 6.91.6.91
45241 6.91 6.91.6.91
55732 119.05 119.05.137.98
55732 137.98 119.05.137.98
df['Fees'] = df['Fees'].astype(float)
df.groupby(['sku'])['Fees'].sum()
# Computes the sum
df.groupby(['sku'])['Fees'].transform('sum')
# Computes the sum but using 'transform' duplicates the value for each row

Extracting numbers from string directly for division [duplicate]

This question already has answers here:
How to flip a column of ratios, convert into a fraction and convert to a float
(3 answers)
Closed 2 years ago.
I have downloaded and created the dataframe below. I would like to create an additional column, in which I divide the second number from a cell by the first one. To give an example, the first cell of the column should be 0.8 (because it's 4/5 = 0.8). Does anyone know how to get the numbers from the string directly and divide them?
Thanks in advance, any help or tips appreciated
Use:
df['Ratio'] = (df['Ratio'].str.split(' for ', expand=True)
.astype(float)
.assign(Ratio= lambda x: x[0] / x[1])['Ratio'])

How can I sort date array [duplicate]

This question already has answers here:
Sort a Python date string list
(2 answers)
Sort list of date strings
(2 answers)
Closed 5 years ago.
Please see my following code snippet
Input
list_x = ["11/1/2100", "5/12/1999", "19/1/2003", "11/9/2001"]
Output
['5/12/1999', '11/9/2001', '19/1/2003', '11/1/2100']
You can convert your day/month format to day in year format and then compare each element in the list based on the year then the day

Does Pandas have notin function to filter rows from data frame from a given list [duplicate]

This question already has answers here:
Find the list values not in pandas dataframe data
(3 answers)
Closed 5 years ago.
I have a data frame and I need to get the rows which are "no in" the given list
I know in order to get the rows from the list we can use isin.(list), so my question is whether there is a contrary "notin" function?
You can use the ~ in front of condition to negate it.
~df['Col1'].isin(list)
df['Col1'].isin(list) will return True/False, then just flip the boolean to get True where Col1 is not in the list.

Categories

Resources