Python 由于将整数作为列索引,Pandas.apply()出现意外结果
让我们看一个最简单的例子:Python 由于将整数作为列索引,Pandas.apply()出现意外结果,python,pandas,Python,Pandas,让我们看一个最简单的例子: In [208]: L={'A':[[1,2]], 'B':[[3,4], [5,6]]} df=pd.DataFrame.from_dict(dict(L), orient="index").stack().reset_index(level=0) df['val']=None print 'before apply. \n\n', df f=lambda x: [x[0], x[1][0], x[1][1]] print '\nafter apply. \n
In [208]:
L={'A':[[1,2]],
'B':[[3,4], [5,6]]}
df=pd.DataFrame.from_dict(dict(L), orient="index").stack().reset_index(level=0)
df['val']=None
print 'before apply. \n\n', df
f=lambda x: [x[0], x[1][0], x[1][1]]
print '\nafter apply. \n\n', df.apply(f, axis=1)
before apply.
level_0 0 val
0 A [1, 2] None
0 B [3, 4] None
1 B [5, 6] None
after apply.
level_0 0 val
0 [1, 2] 1 2
0 [3, 4] 3 4
1 [5, 6] 5 6
奇怪!lambda
函数应该为每一行返回一个列表
:对于第一行,结果应该是['a',1,2]
,因此,apply()
的预期行为应该是:
level_0 0 val
0 A 1 2
0 B 3 4
1 B 5 6
关于
apply()
,我有什么误解吗?因为您的列名为0
(整数),所以x[0]
表示“从名为0
的列获取”,而不是从列号0获取。但是没有名为1
的列,因此x[1]
表示“从列号1获取”
尝试使用x['level\u 0']
f=lambda x: [x['level_0'], x[1][0], x[1][1]]
或者将列a
0
重命名为string“0”
,因为您的列名为0
(整数),所以x[0]
表示“从名为0
的列获取”,而不是从列号0获取。但是没有名为1
的列,因此x[1]
表示“从列号1获取”
尝试使用x['level\u 0']
f=lambda x: [x['level_0'], x[1][0], x[1][1]]
或者将列a
0
重命名为string“0”
,因为您的列名为0
(整数),所以x[0]
表示“从名为0
的列获取”,而不是从列号0获取。但是没有名为1
的列,因此x[1]
表示“从列号1获取”
尝试使用x['level\u 0']
f=lambda x: [x['level_0'], x[1][0], x[1][1]]
或者将列a
0
重命名为string“0”
,因为您的列名为0
(整数),所以x[0]
表示“从名为0
的列获取”,而不是从列号0获取。但是没有名为1
的列,因此x[1]
表示“从列号1获取”
尝试使用x['level\u 0']
f=lambda x: [x['level_0'], x[1][0], x[1][1]]
或者将columna0
重命名为string“0”
行内注释:
>>> ts = df.iloc[0,] # take the first row as an example
>>> ts
level_0 A
0 [1, 2]
val None
Name: 0, dtype: object
>>> ts[0] # `0` is in the index, so it resolves to item with `index` 0
[1, 2]
>>> ts[1] # one is not in the index, so it resolves to ts.iloc[1]
[1, 2]
>>> ts[1][0] # (ts.iloc[1])[0]
1
>>> ts[1][1] # (ts.iloc[1])[1]
2
故事的寓意:不要将整数值用作列名行内注释:
>>> ts = df.iloc[0,] # take the first row as an example
>>> ts
level_0 A
0 [1, 2]
val None
Name: 0, dtype: object
>>> ts[0] # `0` is in the index, so it resolves to item with `index` 0
[1, 2]
>>> ts[1] # one is not in the index, so it resolves to ts.iloc[1]
[1, 2]
>>> ts[1][0] # (ts.iloc[1])[0]
1
>>> ts[1][1] # (ts.iloc[1])[1]
2
故事的寓意:不要将整数值用作列名行内注释:
>>> ts = df.iloc[0,] # take the first row as an example
>>> ts
level_0 A
0 [1, 2]
val None
Name: 0, dtype: object
>>> ts[0] # `0` is in the index, so it resolves to item with `index` 0
[1, 2]
>>> ts[1] # one is not in the index, so it resolves to ts.iloc[1]
[1, 2]
>>> ts[1][0] # (ts.iloc[1])[0]
1
>>> ts[1][1] # (ts.iloc[1])[1]
2
故事的寓意:不要将整数值用作列名行内注释:
>>> ts = df.iloc[0,] # take the first row as an example
>>> ts
level_0 A
0 [1, 2]
val None
Name: 0, dtype: object
>>> ts[0] # `0` is in the index, so it resolves to item with `index` 0
[1, 2]
>>> ts[1] # one is not in the index, so it resolves to ts.iloc[1]
[1, 2]
>>> ts[1][0] # (ts.iloc[1])[0]
1
>>> ts[1][1] # (ts.iloc[1])[1]
2
故事的寓意:不要使用整数值作为列名
故事的士气
是最好的:)好的观点0
索引实际上来自stack()
步骤,因此该部分很糟糕。但我想改名会让事情变得正确。故事的士气
是最好的:)很好0
索引实际上来自stack()
步骤,因此该部分很糟糕。但我想改名会让事情变得正确。故事的士气
是最好的:)很好0
索引实际上来自stack()
步骤,因此该部分很糟糕。但我想改名会让事情变得正确。故事的士气
是最好的:)很好0
索引实际上来自stack()
步骤,因此该部分很糟糕。但我想改名会让事情变好的。