Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用数字列标题切片数据帧时的异常行为_Python_Python 3.x_Pandas_Dataframe_Crosstab - Fatal编程技术网

Python 使用数字列标题切片数据帧时的异常行为

Python 使用数字列标题切片数据帧时的异常行为,python,python-3.x,pandas,dataframe,crosstab,Python,Python 3.x,Pandas,Dataframe,Crosstab,我有一个包含病例对照数据的熊猫数据框,可以用以下结构表示: caseA caseN catA 0 y 1 a 1 y 1 a 2 y 1 b 3 y 1 b 4 y 1 c 5 y 1 d 6 y 1 a 7 y 1 c 8 n 0 c 9 n

我有一个包含病例对照数据的熊猫数据框,可以用以下结构表示:

   caseA  caseN catA
0      y      1    a
1      y      1    a
2      y      1    b
3      y      1    b
4      y      1    c
5      y      1    d
6      y      1    a
7      y      1    c
8      n      0    c
9      n      0    d
10     n      0    a
11     n      0    b
12     n      0    c
13     n      0    a
14     n      0    d
15     n      0    a
16     n      0    b
17     n      0    c
18     n      0    a
19     n      0    d
caseA和caseN变量分别将案例和控件表示为字符串和整数

我可以计算一个2x2表格,以便于使用pandas交叉表法计算优势和优势比。列的默认顺序是control case,但我将其更改为case-control,以我的思维方式,这更直观一些

然后,我对数据帧进行切片,以便在order case-control中只打印选定数量的带有列的行。这完全符合预期

但是,如果我向数据帧添加一个新列(例如,包含赔率值的列),然后以完全相同的方式对数据帧进行切片,则案例和控件的打印顺序错误

以下代码片段说明了这一点:

df = pd.DataFrame({'caseN':[1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0],
                   'caseA':['y','y','y','y','y','y','y','y','n','n','n','n','n','n','n','n','n','n','n','n'],
                   'catA':['a','a','b','b','c','d','a','c','c','d','a','b','c','a','d','a','b','c','a','d']})

print('\nCross tabulation\n')
continTab = pd.crosstab(df['catA'],df['caseN'])
print(continTab)

print('\nReorderd cross tabulation\n')
continTab = continTab[[1,0]]
print(continTab)

#print('\n<-- An extra column containg odds has been entered here -->')
#continTab['odds'] = continTab[1]/continTab[0]

print('\nPrint just a slice contains rows a and c only with 1 - 0 column order\n')
print(continTab.loc[['a','c'],[1,0]])
但如果取消注释计算赔率列的代码,然后重新运行完全相同的代码,则生成的切片表为:

caseN  0  1
catA       
a      4  3
c      3  2
我想不出发生这种事的理由。这是虫子吗


(有趣的是,使用描述为字符串的病例对照数据(在变量caseA中)重复该过程会产生正确的预期结果。)

这里值得问一个问题:我认为你是对的。完成了。值得在这里问一个问题:我认为你是对的。完成。
caseN  0  1
catA       
a      4  3
c      3  2