Created
June 13, 2014 18:21
-
-
Save mmautner/018449566fe6cccccd22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pandas as pd | |
df = pd.read_csv('users.csv') | |
def flatten(user_df): | |
new_user_df = pd.DataFrame() | |
new_user_df['userid'] = user_df.userid | |
for send_number, row in user_df.set_index('send_number').iterrows(): | |
new_user_df['send_%d' % send_number] = row.opened | |
return new_user_df | |
df = df.groupby('userid').apply(flatten) | |
print df.head() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./blah.py | |
send_0 send_1 userid | |
userid | |
1 0 0 0 1 | |
1 0 0 1 | |
2 2 1 0 2 | |
3 1 0 2 | |
3 4 1 NaN 3 | |
[5 rows x 3 columns] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
userid | send_number | opened | |
---|---|---|---|
1 | 0 | 0 | |
1 | 1 | 0 | |
2 | 0 | 1 | |
2 | 1 | 0 | |
3 | 0 | 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment