in Education by
Working with census data, I want to replace NaNs in two columns ("workclass" and "native-country") with the respective modes of those two columns. I can get the modes easily: mode = df.filter(["workclass", "native-country"]).mode() which returns a dataframe: workclass native-country 0 Private United-States However, df.filter(["workclass", "native-country"]).fillna(mode) does not replace the NaNs in each column with anything, let alone the mode corresponding to that column. Is there a smooth way to do this? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can simply use this line of code. cols = ["workclass", "native-country"] df[cols]=df[cols].fillna(df.mode().iloc[0]) or instead of fillna(df.mode().iloc[0]), you can use fillna(mode.iloc[0]) Example: import pandas as pn df={ 'P3': [7,9,9,9,3], 'P2': [8,8,9], 'P1': [8,9,9], } df=pn.DataFrame.from_dict(d,orient='index').transpose() Then df is P3 P2 P1 0 7 8 8 1 9 8 9 2 9 9 9 3 9 NaN NaN 4 3 NaN NaN After this, l=df.filter(["P1", "P2"]).mode() df[["P1", "P2"]]=df[["P1", "P2"]].fillna(value=l.iloc[0]) we get that df is P3 P2 P1 0 7 8 8 1 9 8 9 2 9 9 9 3 9 8 9 4 3 8 9 If you want to be build successful data science career then enroll for best data science certification.

Related questions

0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I created a dataframe and column labels in pandas and I need to edit in order to replace the original column ... names. Please help? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How do I get the index column name in python pandas? Here's an example dataframe: Index Title Column 1 Apples 1 ... how to do this? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I want to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I don ... 'gdp', 'cap'] Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    def multiple_dfs(xyz_file, sheet, *args): row=2 writer = pd.ExcelWriter(xyz_file, engine='openpyxl') df = pd. ... help me over this? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a pandas data frame like: a b A 1 A 2 B 5 B 5 B 4 C 6 I want to group by the ... do something like this using pandas groupby? Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    What exactly is the difference between groupby("x").count and groupby("x").size in Pandas? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have a dataset |category| cat a cat b cat a I'd like to be able to return something like (showing unique values ... cat a 2 cat b 1 Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I have a dataframe as below itm Date Amount 67 420 2012-09-30 00:00:00 65211 68 421 2012-09-09 00 ... solutions would be appreciated. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the following df | 1 | 2 | 3 | ------------------------- 0.11 ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    import pandas as pd from itertools import combinations, product, permutations MH_P= ["Maria Herrera"] OP_P= ["Oscar ... i solve it? Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    An irregular time series data is stored in a pandas.DataFrame. A DatetimeIndex has been set. I need the ... seemingly simple operation? Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
...