简体   繁体   English

基于输入列索引拆分和合并熊猫数据框

[英]split and merge pandas dataframe based on input column index

I have a pandas dataframe test_df .我有一个熊猫数据test_df This dataframe contains 20 columns.此数据框包含 20 列。 I am given a list of column index col_index = [3, 5] for example.例如,我得到了一个列索引col_index = [3, 5]的列表。

I need to create two separate dataframes我需要创建两个单独的数据框

  • one only including the columns in col_index一个只包括 col_index 中的列
  • other includes all the columns except the columns in col_index other 包括除 col_index 中的列之外的所有列

How do I do that?我怎么做?

I understand I can do我明白我能做到

new_df = df.iloc[:, 3] 

To create a dataframe out of column number 3. But what do I do as in this case I have multiple column numbers to separate out from the main dataframe?从第 3 列中创建一个数据框。但是在这种情况下我该怎么做,因为我有多个列号要从主数据框中分离出来?

Using python 3使用蟒蛇 3

You can do with drop你可以用drop

df1=df.iloc[:,col_index].copy()
df2=df.drop(df1.columns.tolist(),axis=1).copy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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