2

I have a list like the one below.

Gamecode    rush    pass
23....89     347    
23....89             650
23....90     654    
23....90             230

the code is below.

temp = {}
for row in combineddff.itertuples():
    temp={}
    if row[1] in ('RUSH', 'PASS'):
        temp['GameCode'] =  row[0]
        if row[1] == 'RUSH':
            temp['rush'] = row[10]
        else:
             temp['pass'] = row[10]

    else:
        continue
    templist.append(temp)

print templist
my_df = pd.DataFrame(templist)
my_df.to_csv('data/results_yards.csv', index=False, header=False)

I want to combine the rush and pass values of separate rows in the templist into one row with "GameCode", "Rush" and "Pass" as values. kindly help.

3
  • Provide information in the question. Not in links and definitely not using images. Commented Feb 12, 2016 at 23:15
  • I understand. I'm sorry. Should I use this post now, or create a new one ? Commented Feb 12, 2016 at 23:20
  • Edit this post. And provide all information that is needed to create a minimal, complete and verifiable example. Better yet - create one yourself and share it so people are motivated to help you: stackoverflow.com/help/mcve Commented Feb 12, 2016 at 23:21

2 Answers 2

1

I'm assuming your columns are 'None' if there is no value.

game_code = 'GameCode'
pass_yds = 'PASS'
rush_yds = 'RUSH'

output_list = []
for row in combineddff.itertuples():
    if row[0] == game_code:
        if row[2] is not None: pass_yds = row[2]
        if row[1] is not None: rush_yds = row[1]
    else:
        output = (game_code, pass_yds, rush_yds)
        output_list.append(output)

# Flush the last group
output = (game_code, pass_yds, rush_yds)
output_list.append(output)

Edit: after comments

templist = [
    { 'GameCode': 'A', 'PASS': '1' },
    { 'GameCode': 'A', 'RUN': '2' },
    { 'GameCode': 'B', 'PASS': '3' },
    { 'GameCode': 'B', 'RUN': '4' },
]

merged = None
output_list = []

for t in templist:
    if merged is None:
        merged = t
    elif merged['GameCode'] == t['GameCode']:
        merged.update(t)
    else:
        output_list.append(merged)
        merged = t
Sign up to request clarification or add additional context in comments.

4 Comments

This is what I've been looking for. Only that instead of using combineddff.itertuples(), I want to use templist from my original post. Here, I'm getting keyerror @ if row[0] == game_code:, if i use templist instead of combineddff.itertuples()
Does your templist contain tuples, lists, or dicts? If it's dicts, you might have to use row['GameCode'] or something.
Yes, each value inside the list is a dict with either (gamecode,pass) or (gamecode,rush) with gamecode and rush are being the key values in dicts. So in your code in the line "if row['GameCode'] == game_code:" Is it checking the string "GameCode" or its key value ?
I have added a different piece of code, based on what I think your templist looks like. Is that more accurate?
1

try to use pd.merge method:

import pandas as pd

rush = pd.DataFrame({'Gamecode': [2389, 2390], 'rush': [347, 654]})
pss = pd.DataFrame({'Gamecode': [2389, 2390], 'pass': [650, 230]})

print(pd.merge(rush, pss, on='Gamecode'))

Output:

   Gamecode  rush  pass
0      2389   347   650
1      2390   654   230

4 Comments

They don't belong to separate data frames. Templist is a list and they belong to separate rows, as in rush value is present in one row and pass value is present in another(As you can see in the example list I have provided). It may be because of my code above. Or do you want me to get them both separately and do the merge ?
it would be easier to help you if you would provide a few rows, representing "combineddff" dataframe. but not having this information and following your logic i presume it would be easier to have two different dataframes and merge them.
I understand. I should have done that. Now it is not allowing me to add a head of my data frame here. Is there any way I can add it here ? Thanks for the help!
@GowrisankarGopalakrishnan, you can publish your text file with the dataframe (input data) in the internet, so another users with higher reputation can add it to your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.