Skip to content
Advertisement

Create new dataframe from an existing dataframe

I have a pandas dataframe with say 6 columns. 3 of the columns are of length 5. Two of the columns are of length 2 and the last column is of length 8. The columns are randomly positioned in the dataframe. I would like to create 3 new dataframes. The first dataframe should only contain all the columns whose length is 5. The second dataframe should only contain the columns with length 2 and the third data frame should only contain column of length 8. How do I do this?

Advertisement

Answer

Let us do this:

df_length_5 = df[[col for col in df.columns if len(df[col].dropna()) == 5]].dropna()
df_length_2 = df[[col for col in df.columns if len(df[col].dropna()) == 2]].dropna()
df_length_8 = df[[col for col in df.columns if len(df[col].dropna()) == 8]].dropna()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement