No.1 pandas recipe collection that can reach the itchy place
I want to manage an array / DataFrame in which "category name (index)" and "value" are one element as a character string. Let's assign a value to a column whose column name is each category (index).
ID | Fucking big string |
---|---|
0 | A 0, B 1, C 2 |
1 | B 3, D 4 |
2 | A 5, C 6 |
3 | C 7, D 8 |
↑ I want to ↓ this ↑ fucking big character string ↓.
ID | A | B | C | D |
---|---|---|---|---|
0 | 0 | 1 | 2 | 0 |
1 | 0 | 3 | 0 | 4 |
2 | 5 | 0 | 6 | 0 |
3 | 0 | 0 | 7 | 8 |
Each category is known. If you don't know, make a list of categories.
str_to_dataframe.py
import numpy as np
import pandas as pd
def str_to_dict(x):
dic = {'A' : 0, 'B' : 0, 'C' : 0, 'D' : 0}
for xx in x.split(","):
xxs = xx.split(" ")
dic[xxs[-2]] = int(xxs[-1])
return dic
temp = df["Fucking big string"].apply(lambda x : pd.Series(str_to_dataframe(x)))
df = pd.concat((df, temp), axis=1)
This is fast!
Recommended Posts