简体   繁体   中英

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM