2021年1月15日星期五

Split string in DataFrame column into several columns

I have a DataFrame which looks as follows:

id  time          activity  4   1596213715048   [{"name":"STILL","conf":100}]  4   1596213739171   [{"name":"STILL","conf":54},{"name":"ON_FOOT","conf":19},{"name":"WALKING","conf":19},{"name":"ON_BICYCLE","conf":9},{"name":"IN_VEHICLE","conf":8},{"name":"UNKNOWN","conf":3}]  4   1596213755797   [{"name":"STILL","conf":97},{"name":"UNKNOWN","conf":2},{"name":"IN_VEHICLE","conf":1}]  6   1596214842817   [{"name":"STILL","conf":100}]  6   1596214931090   [{"name":"STILL","conf":34},{"name":"IN_VEHICLE","conf":28},{"name":"ON_FOOT","conf":15},{"name":"WALKING","conf":15},{"name":"ON_BICYCLE","conf":8},{"name":"UNKNOWN","conf":3}]  8   1596214957246   [{"name":"STILL","conf":100}]  9   1596215304418   [{"name":"STILL","conf":100}]  

I would like to split the activity column according to name. The resulting DataFrame should look like:

id  time          IN_VEHICLE  ON_BICYLE  ON_FOOT  WALKING  RUNNING  TILTING  STILL UNKNOWN   4   1596213715048 0           0          0        0        0        0        100   0  4   1596213739171 8           9          19       19       0        0        54    3  4   1596213755797 1           0          0        0        0        0        97    2  6   1596214842817 0           0          0        0        0        0        100   0  6   1596214931090 28          8          15       15       0        0        34    3  8   1596214957246 0           0          0        0        0        0        100   0  9   1596215304418 0           0          0        0        0        0        100   0  

How can this split be done? The resulting columns are fixed but if still a entry in the activity string does not exist as a column in the resulting DataFrame, a error should be thrown.

https://stackoverflow.com/questions/65745345/split-string-in-dataframe-column-into-several-columns January 16, 2021 at 08:35AM

没有评论:

发表评论