mk2080
mk2080

Reputation: 922

Appending Entire Column into Dictionary

I'm working with a dataframe. If the column in the dataframe has a certain percentage of blanks I want to append that column into a dictionary (and eventually turn that dictionary into a new dataframe).

features = {}
percent_is_blank = 0.4


for column in df: 
    x = df[column].isna().mean()
    if x < percent_is_blank:
        features[column] = ??

new_df = pd.DataFrame.from_dict([features], columns=features.keys())

What would go in the "??"

Upvotes: 1

Views: 50

Answers (1)

jezrael
jezrael

Reputation: 862571

I think better is filtering with DataFrame.loc:

new_df = df.loc[:, df.isna().mean()  < percent_is_blank]

In your solution is possible use:

for column in df: 
    x = df[column].isna().mean()
    if x < percent_is_blank:
        features[column] = df[column]

Upvotes: 1

Related Questions