Python: How do I output a element to a specific column and rows depending on the result of if statement

妖精的绣舞 提交于 2021-01-28 11:05:19

问题


Incorporating with excel, I'm looking for a solution that would copy a specific element to another element depending if isOrganization is true. Using pandas df['isOrganization'] = df['Code'].str.endswith('000') statement, I managed to list true and false result with print function.

If the column isOrganization is true, then the row that is true should be copied from column E and F to column B and C. Else: the row should be copied from column E and F to column D and E

I.E. : This copies the entire column # dfo[['B', 'C']] = df[['E', 'F']] but I would only like to copy single row from the column.

Attempt:

dfo = pd.read_excel('output.xlsx')
df = pd.read_excel('input.xls')

df['ifOrganization'] = df['Code'].str.endswith('000')

for idx, val in enumerate(df['ifOrganization']):
    if val == True:
        print(idx, val) #dfo[['B', 'C']] = df[['E', 'F']]
else:
    print(idx, val) #dfo[['D', 'E']] = df[['E', 'F']]

This is printed for isOrganization output:

This is printed for pf(column) output:

First few element under E and F:


回答1:


Use DataFrame.loc with set columns by mask, convert mask to column is not necessary. Also for match False values is used ~ for inverting mask:

df = pd.read_excel('input.xls')

mask = df['Code'].str.endswith('000', na=False)

df.loc[mask, ['B', 'C']] = df.loc[mask,['F', 'G']].to_numpy()

df.loc[~mask, ['D', 'E']] = df.loc[~mask, ['F', 'G']].to_numpy()

df.to_excel('output.xlsx', index=False)

df = pd.DataFrame({
        'Code':['code000','code001','code002'] * 2,
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
         'F':[7,8] * 3,
         'G':[1,0] * 3
})

If test columns names:

print (df.columns)
Index(['Code', 'B', 'C', 'D', 'E', 'F', 'G'], dtype='object')

mask = df['Code'].str.endswith('000', na=False)

df.loc[mask, ['B', 'C']] = df.loc[mask,['F', 'G']].to_numpy()

df.loc[~mask, ['D', 'E']] = df.loc[~mask, ['F', 'G']].to_numpy()

print (df)
      Code  B  C  D  E  F  G
0  code000  7  1  1  5  7  1
1  code001  5  8  8  0  8  0
2  code002  4  9  7  1  7  1
3  code000  8  0  7  9  8  0
4  code001  5  2  7  1  7  1
5  code002  4  3  8  0  8  0



回答2:


You can use df.loc:

df.loc[df['Code'].str.endswith('000'),['B','C']] = df[['F','G']]

And for the other add ~ to negate condition



来源:https://stackoverflow.com/questions/65087895/python-how-do-i-output-a-element-to-a-specific-column-and-rows-depending-on-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!