如何根据熊猫中的列表过滤DataFrame中的项目?

时间:2020-09-29 07:25:25

标签: python pandas list filter

我是编码的新手,正在尝试处理以下数据:

df=
    Position
    A/C MECHANIC
    A/C TECHNICIAN
    A/C TECHNICIAN HELPER
    ACCOUNTANT
    ACCOUNTANT MANAGER
    ACCOUNTING CLERK
    ACCOUNTS AUDITOR
    ACCOUNTS MANAGER
    ACCOUNTS SUPERVISOR
    ACTING HOSPITAL ADMINISTRATOR
    ADMINISTRATION SECRETARY
    ADMINISTRATIVE  SUPERVISOR
    ADMINISTRATIVE CLERK
    ADMINISTRATIVE COORDINATOR
    ADMINISTRATIVE DIRECTOR
    ADMINISTRATIVE MANAGER
    ADMINISTRATOR OF MED.INSURANCE
    ADMINSTRATION OFFICE MANAGER
    ADMISSION COUNTER CLERK
    ADMISSION OFFICER

我有以下列表:

name=['TECHNICIAN', 'MANAGER', 'CLERK', 'AUDITOR', 'SUPERVISOR', 'SECRETARY', 'COORDINATOR', 'DIRECTOR', 'OFFICER', 'SPECIALIST', 'PROGRAMMER', 'TYPIST', 'LIASON', 'DESIGNER', 'ENGINEER', 'ACCOUNTANT', 'ADMINISTRATOR', 'BAKER', 'COOK']

我正在尝试创建一个新的数据框,它从上述列表中获取值,找到包含该单词的相应位置,然后将其添加到新数据框的列中。

这是我正在使用的代码。

newdf=pd.DataFrame()
for i in name:
  print(i)
  newdf[i]=df[df['position'].str.contains(i)]

我正在尝试将每个过滤后的值添加到“ newdf”中的新列中。

当我运行上述代码时,出现此错误:

ValueError: Cannot set a frame with no defined index and a value that cannot be converted to a Series

我正在尝试获得以下输出:

TECHNICIAN,              MANAGER,
A/C TECHNICIAN           ACCOUNTANT MANAGER
ALUMINUM TECHNICIAN      ACCOUNTS MANAGER
ANAESTHESIA TECHNICIAN   ADMINISTRATIVE MANAGER
APPLIANCE TECHNICIAN    
BIOMEDICAL SENIOR   
BIOMEDICAL TECHNICIAN   
BOILER TECHNICIAN   
COMPUTER TECHNICIAN 
COMPUTER TECHNICIAN 
COMPUTER TECHNICIAN

1 个答案:

答案 0 :(得分:0)

创建DataFrame的字典并传递到concat

dfs = {i: df.loc[df['Position'].str.contains(i), 'Position'].reset_index(drop=True) 
           for i in name}
newdf = pd.concat(dfs, axis=1)

print (newdf)
                 TECHNICIAN                           MANAGER  \
0             A/C TECHNICIAN                ACCOUNTANT MANAGER   
1      A/C TECHNICIAN HELPER                  ACCOUNTS MANAGER   
2                        NaN            ADMINISTRATIVE MANAGER   
3                        NaN      ADMINSTRATION OFFICE MANAGER   

                         CLERK               AUDITOR  \
0             ACCOUNTING CLERK      ACCOUNTS AUDITOR   
1         ADMINISTRATIVE CLERK                   NaN   
2      ADMISSION COUNTER CLERK                   NaN   
3                          NaN                   NaN   

                       SUPERVISOR                     SECRETARY  \
0             ACCOUNTS SUPERVISOR      ADMINISTRATION SECRETARY   
1      ADMINISTRATIVE  SUPERVISOR                           NaN   
2                             NaN                           NaN   
3                             NaN                           NaN   

                      COORDINATOR                     DIRECTOR  \
0      ADMINISTRATIVE COORDINATOR      ADMINISTRATIVE DIRECTOR   
1                             NaN                          NaN   
2                             NaN                          NaN   
3                             NaN                          NaN   

                 OFFICER SPECIALIST PROGRAMMER TYPIST LIASON DESIGNER  \
0      ADMISSION OFFICER        NaN        NaN    NaN    NaN      NaN   
1                    NaN        NaN        NaN    NaN    NaN      NaN   
2                    NaN        NaN        NaN    NaN    NaN      NaN   
3                    NaN        NaN        NaN    NaN    NaN      NaN   

  ENGINEER              ACCOUNTANT                       ADMINISTRATOR BAKER  \
0      NaN              ACCOUNTANT       ACTING HOSPITAL ADMINISTRATOR   NaN   
1      NaN      ACCOUNTANT MANAGER      ADMINISTRATOR OF MED.INSURANCE   NaN   
2      NaN                     NaN                                 NaN   NaN   
3      NaN                     NaN                                 NaN   NaN   

  COOK  
0  NaN  
1  NaN  
2  NaN  
3  NaN  

newdf.to_csv('file.csv', index=False)