数组 Python 拆分 str(太多值无法解压缩)



数组 python split str (值太多,无法解压缩(

df.timestamp[1]
Out[191]:
'2016-01-01 00:02:16' 
#i need to slept these into to feature 
split1,split2=df.timestamp.str.split(' ')
Out[192]:
ValueErrorTraceback (most recent call last)
<ipython-input-216-bbe8e968766f> in <module>()
----> 1 split1,split2=df.timestamp.str.split(' ')
ValueError: too many values to unpack

使用str[index] 由于您正在拆分序列,因此输出也将是一个系列,而不是熊猫中的两个不同的列表。

df = pd.DataFrame({'timestamp':['2016-01-01 00:02:16','2016-01-01 00:02:16'] })
split1,split2  = df.timestamp.str.split(' ')[0], df.timestamp.str.split(' ')[1]

例如,str.split将返回一个序列

df.timestamp.str.split(' ')
0    [2016-01-01, 00:02:16]
1    [2016-01-01, 00:02:16]
Name: timestamp, dtype: object

你用错split()方法。

鉴于此:

df.timestamp[1]
Out[191]:
'2016-01-01 00:02:16'

使用split()如下方法:

# I need to split timestamp[1]
split1, split2 = df.timestamp[1].split(' ')  # remove str.

最新更新