'Extracting from dataframe
I have a pandas dataframe that looks like this:
letter;Pairs;Count
abandon;frozenset(['abandon', 'dm']);1
abattoir;frozenset(['abattoir', 'year']);1
abbey;frozenset(['abbey', 'mean']);1
I want to write to a csv that looks like:
abandon;dm
abbattoir;year
abbey;mean
Standard pandas dataframe selection does not seem to work as frozensetcomplicates things.
Solution 1:[1]
You can do this...
df["Pairs"].apply(lambda x: list(x)[0]).astype("unicode")
Solution 2:[2]
I think that
print(X.apply(lambda x: ";".join(x[1]),axis=1).to_csv(index=False))
or
print(X.apply(lambda x: ";".join(x.Pairs),axis=1).to_csv(index=False))
where X is your dataframe might work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jhonsfran |
| Solution 2 | yeharav |
