将空值添加到 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26805445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding null values to a pandas dataframe
提问by darkpool
I have a pandas dataframe that is used to create a JSON which in turn is used to display a highcharts chart.
我有一个用于创建 JSON 的 Pandas 数据框,而后者又用于显示 highcharts 图表。
Pandas dataframe:
Pandas数据框:
Date colA colB
12-Sep-14 20 40
13-Sep-14 50 10
14-Sep-14 12 -20
15-Sep-14 74 43
Is there a way to change some of the colA and colB values to null. The reason for this is that I ultimately need a JSON that looks something like this:
有没有办法将某些 colA 和 colB 值更改为 null。这样做的原因是我最终需要一个看起来像这样的 JSON:
[
[12-Sep-14, 20, 40],
[13-Sep-14, null, null],
[14-Sep-14, 12, -20],
[15-Sep-14, 74, 43]
]
The reason for this is that I require a highcharts chart where certain plot points are blank. To do this, you specify the date followed by null.
这样做的原因是我需要一个 highcharts 图表,其中某些绘图点是空白的。为此,您指定日期后跟空值。
So I need to somehow update certain values in the pandas dataframe so that once I convert it to a JSON using .to_json() then the json will contain the specified null values as per the example above.
所以我需要以某种方式更新 pandas 数据帧中的某些值,以便一旦我使用 .to_json() 将其转换为 JSON,那么 json 将包含指定的空值,如上例所示。
Thanks for any suggestions.
感谢您的任何建议。
采纳答案by user308827
Does this work?
这行得通吗?
import pandas as pd
# Read in data frame from clipboard
df = pd.read_clipboard()
df = df.replace(df.iloc[1][1:],'null')
Date colA colB
0 12-Sep-14 20 40
1 13-Sep-14 null null
2 14-Sep-14 12 -20
3 15-Sep-14 74 43
Here, df.iloc[1] gives access to row 1
在这里,df.iloc[1] 可以访问第 1 行
Finally,
最后,
df.to_json(orient='values').replace("\"","")
gives json without the ""
给出没有“”的json
[[12-Sep-14,20,40],[13-Sep-14,null,null],[14-Sep-14,12,-20],[15-Sep-14,74,43]]
回答by JD Long
Try using NaN which is the Pandas missing value:
尝试使用 NaN,这是 Pandas 的缺失值:
df = pd.read_clipboard()
df.colA.iloc[1] = NaN
instead of NaN you could also use None. Note that neither of these terms are entered with quotes.
除了 NaN,您还可以使用 None。请注意,这些术语均不带引号。
Then you can use to_json() to get your output:
然后你可以使用 to_json() 来获取你的输出:
df.to_json()
'{"Date":{"0":"12-Sep-14","1":"13-Sep-14","2":"14-Sep-14","3":"15-Sep-14"},"colA":{"0":20.0,"1":null,"2":12.0,"3":74.0},"colB":{"0":40,"1":10,"2":-20,"3":43}}'

