Below is the Dataframe i’m working. I want to replace NaN values in ‘Score’ columns using values from column ‘Country’ and ‘Sectors’ Below is the code which I’ve tried I want to replace only NaN values specific to country == ‘USA’ and Sectors == ‘CHEM’ and keep all values as it is. Could anyone please help?“` Answer You can use
Tag: dataframe
Python: How to One Hot Encode a Feature with multiple values?
I have the following dataframe df with names of the travelling cities in route column of an aircraft with it’s ticket_price. I want to obtain individual city names from route and one hot encode them. Dataframe (df) Required Dataframe (df_encoded) Code I have performed some preprocessing on the route column using the following code but am unable to understand how
How to drop rows in one DataFrame based on one similar column in another Dataframe that has a different number of rows
I have two DataFrames that are completely dissimilar except for certain values in one particular column: How would I go about finding the matching values in the Email column of df and the Contact column of df2, and then dropping the whole row in df based on that match? Output I’m looking for (index numbering doesn’t matter): I’ve been able
Python Regex DataFrame match
I have a DataFrame and I would like to perform a sorting if the match between my regex and the name of one of the lines of this DataFrame matches. Is there an option in the “re” library to help me? I try with this piece of code but without success Thank you in advance for your answers Answer I
pandas .diff() but use first cell as difference between last cell in prior column
say that i have a df in the following format: and i would like to get the difference of the 2020 column by using df[‘delta’] = df[‘2020’].diff() this will obviously return NaN for the first value in the column. how can i make it so that it automatically interprets that diff as the difference between the FIRST value of 2020
Pandas merge indexing not behaving as expected
I am trying to perform an anti-join in effectively one line. However, my one line solution is not giving me the same results that a receive when breaking up the code into two lines (which behaves as expected). Specifically, the single-line solution results in a dataframe with fewer rows. The goal of my anti-join is to remove any overlap of
Pandas deleting rows based on same sting in columns
Hello i am using pandas DataFrame to clean this file and want to delete rows which contains the manufacturers name in the buy-box seller column. For example row 1 will be deleted because it contains the string ‘Goli’ in Buy-Box seller Column. Answer There are misisng values so first replace them by DataFrame.fillna and then test if match values between
match dtypes of one df to another with different number of columns
I have a dataframe that has 3 columns and looks like this: The other dataframe looks like this: I need to match the data types of one df to another. Because I have one additional column in df_1 I got an error. My code looks like this: I got an error: KeyError: ‘profitable’ What would be a workaround here? I
Create multiple new rows per row in data frame
I have the following df: What I want to do know is add x new rows per row based on the id. So more specific spoken, I want to add a new column containing the date from a range of 7 days and then add a new row with the date for every ID in the df. so the output
Manipulate string to drop columns on pandas
I’m trying to manipulate a list (type: string) to use that list to drop some columns from a dataframe. Dataframe The list is from a dataframe that I created a condition to return columns whose sums of all values are zero: Selecting the columns with sum = 0 Importing the dataframe and turning it into a list: Images from the