我在熊猫中有一个数据框,其中包含几列,我需要访问包含一个项目列表的一个单元格,怎么可能? (例如,下面的示例中如何访问Match元素)
ID Match
1 (word1,,,)
2 (word2,,,),(word1)
3 (word2,,,),(word1),(word3,,,)
答案 0 :(得分:1)
我建议使用type Door = {
id: number,
rotation: number
}
type Handle = {|
id: number
|}
type Entity = Handle | Door;
const foo = (entity: Entity): number => {
if (entity.rotation) {
return entity.rotation;
} else {
return 2;
}
}
索引器,如果由于值不存在而不匹配,则返回str
:
NaN
示例:
#if need slect first tuple
df['new'] = df['Match'].str[0]
#if need select second tuple and first element of tuple
df['new'] = df['Match'].str[1].str[0]
编辑:
如果值是字符串,请使用a = [[('word1','','','')],
[('word2','','',''),('word1', )],
[('word2','','',''),('word1', ),('word3','','','')]]
df = pd.DataFrame({'ID':[1,2,3], 'Match':a})
df['new1'] = df['Match'].str[0]
df['new2'] = df['Match'].str[1].str[0]
print (df)
ID Match new1 new2
0 1 [(word1, , , )] (word1, , , ) NaN
1 2 [(word2, , , ), (word1,)] (word2, , , ) word1
2 3 [(word2, , , ), (word1,), (word3, , , )] (word2, , , ) word1
:
ast.literal_eval