如果我正确理解您的问题,我相信它将填充所有列中的 NaN 值。
来自 [http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html][1]
import pandas as pd
index = ['Firefox', 'Chrome', 'Safari', 'IE10', 'Konqueror']
new_index= ['Safari', 'Iceweasel', 'Comodo Dragon', 'IE10','Chrome']
df = pd.DataFrame({
'http_status': [200,200,404,404,301],
'response_time': [0.04, 0.02, 0.07, 0.08, 1.0]},
index=index)
df
返回:
http_status response_time
Firefox 200 0.04
Chrome 200 0.02
Safari 404 0.07
IE10 404 0.08
Konqueror 301 1.00
df.reindex(new_index, fill_value='missing') 返回时:
http_status response_time
Safari 404 0.07
Iceweasel missing missing
Comodo Dragon missing missing
IE10 404 0.08
Chrome 200 0.02
这些列都不是新的,但仍然填写了 nan 值。我肯定会在投入生产之前测试我的解释。我不确定我是否有正确的上下文。
编辑:
我应该补充一点,好像以前的值是“NaN”,.reindex 不会填充这些值:
import pandas as pd
index = ['Firefox', 'Chrome', 'Safari', 'IE10', 'Konqueror']
new_index= ['Safari', 'Iceweasel', 'Comodo Dragon', 'IE10','Chrome']
df = pd.DataFrame({
'http_status': [200,'NaN',404,404,301],
'response_time': [0.04, 0.02, 0.07, 0.08, 1.0]},
index=index)
df
返回:
http_status response_time
Safari 404 0.07
Iceweasel NaN NaN
Comodo Dragon NaN NaN
IE10 404 0.08
Chrome NaN 0.02
虽然 df.reindex(new_index, fill_value='missing') 返回:
http_status response_time
Safari 404 0.07
Iceweasel missing missing
Comodo Dragon missing missing
IE10 404 0.08
Chrome NaN 0.02
HTTP Status-Chrome 值不受切换索引的影响。