Converting a pandas DF to JSON without NAN
我有一个df,看起来像:
1 2 3 | A B C D E 1 1 NaN 1 1 NaN 2 3 4 NaN |
当我这样做的时候:
1 | df.to_json(orient='records') |
我有点像
1 | [{"A":1,"B":1,"C":null,"D":1,"E":1},{"A":null,"B":2,"C":3,"D":4,"E":null}] |
是否可以让它忽略任何有nan的内容,并显示如下内容:
1 | [{"A":1,"B":1,"D":1,"E":1},{"B":2,"C":3,"D":4}] |
我可以用熊猫做这个吗?
试试这个:
1 2 3 | [{**x[i]} for i, x in df.stack().groupby(level=0)] [{'A': 1.0, 'B': 1.0, 'D': 1.0, 'E': 1.0}, {'B': 2.0, 'C': 3.0, 'D': 4.0}] |
如果你想要的话
1 2 3 | [{**x[i]} for i, x in df.stack().map(int).groupby(level=0)] [{'A': 1, 'B': 1, 'D': 1, 'E': 1}, {'B': 2, 'C': 3, 'D': 4}] |
如果是
1 2 3 | [{**x[i]} for i, x in df.stack().fillna(0, downcast='infer').groupby(level=0)] [{'A': 1, 'B': 1, 'D': 1, 'E': 1}, {'B': 2, 'C': 3, 'D': 4}] |
解释
1 2 3 4 5 6 7 | # Series with a # MultiIndex Make a Series and drop nulls # ↓ ↓ ↓ Essentially grouping by `index` of `df` [{**x[i]} for i, x in df.stack().groupby(level=0)] # ↑ ↑ # ↑ Slice the MultiIndex with name of the group # Unpack in a dictionary context with double splat `{**mydict} == mydict` |
以下是以前的一个解决方案,用于在DICITONARY键的值为空时删除它们:
1 | {k: v for k, v in metadata.items() if v is not None} |
https://stackoverflow.com/a/12118700/8265971
对于大熊猫,有一个