简体   繁体   English

如何通过pandas数据框解析,根据其他两列的值创建新列

[英]How to parse through pandas dataframe, make new column based on the value of two other columns

I would like to create a new column 'column_new' based on values from column_1 and column_2 or column_3.我想根据 column_1 和 column_2 或 column_3 的值创建一个新列“column_new”。 If column_1 == 'C', then column_new is same value as column_2, but if column_1 == 'G', then column_new is same value as column_3.如果 column_1 == 'C',则 column_new 与 column_2 的值相同,但如果 column_1 == 'G',则 column_new 与 column_3 的值相同。

I have tried:我试过了:

def new_value(x):
   if df1['column_1'] == 'C' :
      return df1['column_2']
   if df1['column_1'] == 'G':
      return df1['column_3']
   else:
       return 'Other'

df1['column_new'] = df1['column_1'].apply(new_value)

error: ValueError: The truth value of a Series is ambiguous.错误:ValueError:系列的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

Have also tried:也试过:

for row in df1:
    if df1.loc[df1['column_1'] == 'C']:
        df1['column_new'] = df1['column_2']
    elif df1.loc[df1['column_1'] == 'G']:
        df1['column_new'] = df1['column_3']

error: ValueError: The truth value of a DataFrame is ambiguous.错误:ValueError:DataFrame 的真值不明确。 Use a.empty, a.bool(), a.item(), a.any() or a.all().使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

Some data:一些数据:

column_1    column_2    column_3
C   AAAACCCACCT ACCCA
C   GTGGGCTAAAA GGCTA
G   ATGGGCTTTTT GGCTT
G   AGAAAGCCCGC AAGCC

Try np.select试试np.select

cond_1 = df['column_1'] == 'C'
cond_2 = df['column_1'] == 'G'
df['column_new'] = np.select([cond_1, cond_2], [df.column_2, df.column_3], 'Other')

Out[1715]:
  column_1     column_2 column_3   column_new
0        C  AAAACCCACCT    ACCCA  AAAACCCACCT
1        C  GTGGGCTAAAA    GGCTA  GTGGGCTAAAA
2        G  ATGGGCTTTTT    GGCTT        GGCTT
3        G  AGAAAGCCCGC    AAGCC        AAGCC

Figured it out:弄清楚了:

def new_value(column_1,column_2, column_3):
    if column_1 == 'C':
        return column_2[:]
    elif column_1 == 'G':
        return column_3[:]
    else:
        return 'NaN'

df1['column_new'] = df1.apply(lambda row: new_value(row.column_1, row.column_2, row.column_3), axis = 1)

您可以尝试:希望它会起作用

df['col_new']=df[(df['col2'][df['col1']=='C']) & (df['col3'][df['col1']=='G']) 

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

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