Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to_dict functionality to make pandas conversion easier #8

Open
rsdefever opened this issue Jul 27, 2020 · 1 comment
Open

Add to_dict functionality to make pandas conversion easier #8

rsdefever opened this issue Jul 27, 2020 · 1 comment
Milestone

Comments

@rsdefever
Copy link
Owner

From Parashara:

If someone did want to convert Elements to a pandas dataframe or other data structure, including a to_dictionary function in Elements which just returns a copy of the symbol_dict would make that easier. Or add element.symbol_dict to the init.py

@ahy3nz
Copy link

ahy3nz commented Jul 27, 2020

import element.element
import pandas as pd
import json

with open(element.element.JSON_PATH) as jsonfile:
    df = pd.DataFrame(json.load(jsonfile)).transpose()

print("Search by name")
print(df.set_index('name').loc['yttrium'])
print('\n')

print("Search by atomic number")
print(df.set_index('atomic number').loc[117])
print('\n')

print("Search by symbol")
print(df.set_index('symbol').loc["Rh"])
print('\n')       

Yields

Search by name
symbol                 Y
atomic number         39
mass             88.9059
Name: yttrium, dtype: object


Search by atomic number
name      ununseptium
symbol            Uus
mass              291
Name: 117, dtype: object


Search by symbol
name             rhodium
atomic number         45
mass             102.906
Name: Rh, dtype: object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants