Skip to content

Instantly share code, notes, and snippets.

@aldanor
Created December 5, 2012 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldanor/4213001 to your computer and use it in GitHub Desktop.
Save aldanor/4213001 to your computer and use it in GitHub Desktop.
Pandas histogram demo
import pandas as pd
def parse_to_df(files):
rank_values = dict(zip(tuple('23456789TJQKA'), range(13)))
records = []
for hand in h_iterators.HandsIterator(
hh_iterator=h_iterators.HandHistoryIterator(files)):
for i, card in enumerate(hand['hero']['cards'].split(' ')):
records.append({'card_rank': (rank_values[card[0]], card[0]),
'card_suit': card[1],
'hand_id': hand['header']['id'], 'n_card': i})
df = pd.DataFrame(records,
columns=['card_rank', 'card_suit', 'hand_id', 'n_card'])
df.set_index(['hand_id', 'n_card'], inplace=True)
return df
def main():
df = parse_to_df(['../../hands/test_sample/File1.txt'])
print df.head(8)
counts = df.groupby('card_rank').apply(len) * 1.
print '\n\n', pd.DataFrame({'counts': counts,
'counts_norm': counts / len(df)})
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment