Skip to content

Question about the problem encountered in "Cubic Crystal Test.ipynb" under the "examples" folder in the original M3GNet #99

Closed Answered by janosh
Claudia-Hello asked this question in Q&A
Discussion options

You must be logged in to vote

in the future, such questions are better asked on stackoverflow.

you can pass any custom function to pd.DataFrame.map(). here's an example of converting the first value in each string. you could change the function to return tuples with all floats instead.

import re
import pandas as pd

data = pd.read_html("http://en.wikipedia.org/wiki/Lattice_constant")[0]

def extract_first_float(value: str) -> float:
    try:
        return float(value)
    except ValueError:
        match = re.search(r"[-+]?\d*\.\d+|\d+", value)
        if match:
            return float(match.group())
        else:
            return float("nan")

data["a (Å)"] = data["a (Å)"].map(extract_first_float)

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by janosh
Comment options

You must be logged in to vote
2 replies
@janosh
Comment options

@Claudia-Hello
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
help Q&A support issues
2 participants