简体   繁体   English

如何在 pandas dataframe 中的列内操作列表名称

[英]How can I Manipulate list names inside columns in pandas dataframe

I have a DataFrame:我有一个 DataFrame:

               RR                    AA                  SS         LL
 C1     [C1, C2, C3, C4, C5]        [C1]                [C1]    
 C2     [C2, C3, C5]            [C1, C2, C3, C5]    [C5, C3, C2]    I
 C3     [C2, C3, C4, C5]        [C1, C2, C3, C5]    [C5, C3, C2]    
 C4           [C4]              [C1, C3, C4, C5]        [C4]        I
 C5     [C2, C3, C4, C5]        [C1, C2, C3, C5]    [C5, C3, C2]    

I want to delete the entire row having LL I ie, rows C2 and C4 Also need to delete the elements C2 and C4 from the remaining rows lists in RR , AA and SS so that the output should be like this:我想删除具有 LL I的整行,即C2C4行还需要从RRAASS中的剩余行列表中删除元素C2C4 ,这样 output 应该是这样的:

            RR               AA            SS         LL
 C1     [C1, C3, C5]        [C1]          [C1]  
 C3     [C3, C5]        [C1, C3, C5]    [C5, C3]    
 C5     [C3, C5]        [C1, C3, C5]    [C5, C3]    

I tried this code but it only deletes the rows not C2 and C4 from list elements in RR , AA and SS .我尝试了这段代码,但它只从RRAASS的列表元素中删除了不是C2C4的行。

ix = df.RS.apply(set) == df.IS.apply(set)
df.loc[~ix]

I am getting output like this where in RR , AA and SS , C2 and C4 are present in their lists which I don't need.我得到这样的 output,其中RRAASSC2C4出现在我不需要的列表中。

               RR                    AA                  SS         LL
 C1     [C1, C2, C3, C4, C5]        [C1]                [C1]    
 C3     [C2, C3, C4, C5]        [C1, C2, C3, C5]    [C5, C3, C2]    
 C5     [C2, C3, C4, C5]        [C1, C2, C3, C5]    [C5, C3, C2]    

This should do it:这应该这样做:

new_df = df.loc[df['LL'] != 'I', ['RR', 'AA', 'SS']].applymap(set).apply(lambda col: col - {'C2', 'C4'}).applymap(list)

Output: Output:

>>> new_df
              RR            AA        SS
C1  {C5, C3, C1}          {C1}      {C1}
C3      {C5, C3}  {C1, C5, C3}  {C5, C3}
C5      {C5, C3}  {C1, C5, C3}  {C5, C3}
col1 = ['C1','C2','C3','C4','C5']
RR = [['C1', 'C2', 'C3', 'C4', 'C5'], ['C2', 'C3', 'C5'], ['C2', 'C3', 'C4', 'C5'], 
        ['C4'], ['C2', 'C3', 'C4', 'C5']]
AA = [['C1'], ['C1', 'C2', 'C3', 'C5'], ['C1', 'C2', 'C3', 'C5'], ['C1', 'C3', 'C4', 'C5'], 
        ['C1', 'C2', 'C3', 'C5']]
SS = [['C1'], ['C5', 'C3', 'C2'], ['C5', 'C3', 'C2'], ['C4'], ['C5', 'C3', 'C2']]
LL = ['','I','','I','']

df1 = pd.DataFrame({'col1':col1, 'RR':RR,'AA':AA, 'SS':SS, 'LL':LL})

removing_row = df1.loc[df1['LL'] == 'I', 'col1']
removing_index = list(removing_row.index)
removing_values = removing_row.values

df1.drop(df1.index[removing_index], inplace=True, axis=0)

for col in ['RR','AA','SS']:
    for i,j in df1[col].iteritems():
        for k in removing_values:
            if k in j:
                j.remove(k)
        df1[col][i] = j

print(df1)

暂无
暂无

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

相关问题 如何操作python列表并将其转换为pandas数据帧? - How can I manipulate python list and convert it to pandas dataframe? 如何使用 pandas dataframe 操作列表? - How can I manipulate list using pandas dataframe? 如何将具有相似名称的列的 pandas dataframe 转换为行? - How can I turn a pandas dataframe with columns with similar names into rows? 如何为 pandas dataframe 生成连续名称列表 - How can I generate a list of continuous names for a pandas dataframe 我如何获取两个列表 Pandas DataFrame 列名,并且只使用一个列表,但 append 一个循环中的字符串应用于列名? - How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a string in a loop to be applied to the column name? 如何将列表解析为 pandas dataframe 中的列 - How can I parse a list to columns in a pandas dataframe 如何将 Pandas DataFrame 的列转换为列表列表? - How can I convert columns of a pandas DataFrame into a list of lists? 如何操作数据框,以便我访问单元格内列表中的每个元素并根据另一列对它们进行分组? - How can manipulate a dataframe such that i access every element in a list inside a cell and group them according to another column? 如何将熊猫数据框作为arg传递并对其进行操作? - How can I pass a pandas dataframe as an arg and manipulate it? 如何将以下 JSON 操作到所需的 Pandas Dataframe 中? - How can I manipulate the following JSON into the desired Pandas Dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM