英文:
Counting the number of times 3 different strings appear over numerous columns and putting this count in a new column
问题
I've translated the code part for you as requested:
df['Received_money'] = df[['Job1', 'Job2']].apply(lambda row: len(row[row == 'Yes']), axis=1)
If you have any more translation requests or need assistance with anything else, please feel free to ask.
英文:
I'm trying to create a new column that tallys up the number of times someone was paid for a job - regardless of if it was all of the money or just some of the money. So for each row, if it says "yes" or "partial" or "paid" in the job columns then I want a count of this in the new column.
My actual data has 15 different job columns that I want to "sum" across.
So before looks like:
Name | Job1 | Job2 |
---|---|---|
tom | Yes | No |
nick | Partial | Yes |
juli | No | No |
And I'd like afterwards to look like:
Name | Job1 | Job2 | Received_money |
---|---|---|---|
tom | Yes | No | 1 |
nick | Partial | Yes | 2 |
juli | No | No | 0 |
Current code
df['Received_money'] = df[['Job1', 'Job2']].apply(lambda row: len(row[row == 'Yes']), axis=1)
This is my current code and it partially does what I want. It adds up the number of times it says "Yes" in the columns listed. But:
- I can't figure out how to expand this to include
"== 'partial'"
and"== 'paid'"
, and how to get it to give 1 point (so to speak) for each time these occur - Is there any other way of entering in all 15 of my column names instead of [['Job1', 'Job2', 'Job3', 'Job4', 'Job5'....'Job15' ]]
(Example data)
import pandas as pd
# initialize list of lists
data = [['tom', "Yes", "No"], ['nick', "Partial", "Yes"], ['juli', "No", "No"]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns=['Name', 'Job1', 'Job2'])
Thank you!
答案1
得分: 1
你的方法看起来没问题。只需像这样添加其他过滤器:
df['Received_money'] = df[['Job1', 'Job2']].apply(lambda row: len(row[row == 'Yes']) + len(row[row == 'Partial']), axis=1)
英文:
Your approach seems okay. Just add the other filters like this:
df['Received_money'] = df[['Job1', 'Job2']].apply(lambda row: len(row[row == 'Yes']) +len(row[row == 'Partial']), axis=1)
答案2
得分: 1
我添加了2个额外的工作列,只是为了测试。这应该满足您的要求。
data = [['tom', "Yes", "No", "Partial", "Paid"], ['nick', "Partial", "Yes"], ['juli', "No", "No", "Partial", "Paid"]]
df = pd.DataFrame(data, columns=['Name', 'Job1', 'Job2', 'Job3', 'Job4'])
job_cols = ['Job1', 'Job2', 'Job3', 'Job4']
paid_values = ['Yes', 'Paid', 'Partial']
df['Received_money'] = df[job_cols].apply(lambda row: len([r for r in row if r in paid_values]), axis=1)
print(df)
英文:
I added 2 more Job columns just for testing. This should satisfy your requirement.
data = [['tom', "Yes", "No","Partial","Paid"], ['nick', "Partial", "Yes"], ['juli', "No", "No","Partial","Paid"]]
df = pd.DataFrame(data, columns=['Name', 'Job1', 'Job2','Job3','Job4'])
job_cols =['Job1','Job2','Job3','Job4']
paid_values = ['Yes','Paid','Partial']
df['Received_money'] = df[job_cols].apply(lambda row : len([r for r in row if r in paid_values]),axis=1)
print(df)
答案3
得分: 1
不要使用 apply
,您可以轻松向量化此操作:
df['Received_money'] = df.filter(like='Job').isin(['Yes', 'Partial']).sum(axis=1)
或者,如果 Job 列不是以字面意义上的 "Job" 开头:
cols = ['Job1', 'Job2']
df['Received_money'] = df[cols].isin(['Yes', 'Partial']).sum(axis=1)
输出结果:
Name Job1 Job2 Received_money
0 tom Yes No 1
1 nick Partial Yes 2
2 juli No No 0
英文:
Don't use apply
, you can easily vectorize this:
df['Received_money'] = df.filter(like='Job').isin(['Yes', 'Partial']).sum(axis=1)
Or, if the Job columns don't start with a literal "Job":
cols = ['Job1', 'Job2']
df['Received_money'] = df[cols].isin(['Yes', 'Partial']).sum(axis=1)
Output:
Name Job1 Job2 Received_money
0 tom Yes No 1
1 nick Partial Yes 2
2 juli No No 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论