我想出了方括号中的值(更像是一个 list
)申请后 str.findall()
到pandas数据帧的列。如何拆除方括号?
print df
id value
1 [63]
2 [65]
3 [64]
4 [53]
5 [13]
6 [34]
我想出了方括号中的值(更像是一个 list
)申请后 str.findall()
到pandas数据帧的列。如何拆除方括号?
print df
id value
1 [63]
2 [65]
3 [64]
4 [53]
5 [13]
6 [34]
如果列中的值 value
有类型 list
, 使用:
df['value'] = df['value'].str[0]
要么:
df['value'] = df['value'].str.get(0)
文件。
样品:
df = pd.DataFrame({'value':[[63],[65],[64]]})
print (df)
value
0 [63]
1 [65]
2 [64]
#check type if index 0 exist
print (type(df.loc[0, 'value']))
<class 'list'>
#check type generally, index can be `DatetimeIndex`, `FloatIndex`...
print (type(df.loc[df.index[0], 'value']))
<class 'list'>
df['value'] = df['value'].str.get(0)
print (df)
value
0 63
1 65
2 64
如果 strings
使用 str.strip
然后转换为数字 astype
:
df['value'] = df['value'].str.strip('[]').astype(int)
样品:
df = pd.DataFrame({'value':['[63]','[65]','[64]']})
print (df)
value
0 [63]
1 [65]
2 [64]
#check type if index 0 exist
print (type(df.loc[0, 'value']))
<class 'str'>
#check type generally, index can be `DatetimeIndex`, `FloatIndex`...
print (type(df.loc[df.index[0], 'value']))
<class 'str'>
df['value'] = df['value'].str.strip('[]').astype(int)
print (df)
value
0 63
1 65
2 64