我有一个包含单个列的大数据,其中每一行的格式为:
82283343~Electronics~Mobile Cases & Covers
我想将上面的列在波浪线分成三列(82283343
,Electronics
,Mobile Case & Covers
)。我写了以下代码:
df= df._id.map(lambda x: x.split('~'))
但这根本没有效率,我最终关闭了终端。还有更好的方法吗?
答案 0 :(得分:1)
我尝试做一些测试并选择最佳方法。
最快的一个是从列_id
创建列表并由原生python split('~')
拆分:
df[['one', 'two', 'three']] = pd.DataFrame([ x.split('~') for x in df['_id'].tolist() ])
import pandas as pd
#test list
x =['82283344~Electronics~Mobile Cases & Covers', '82283346~Electronics~Mobile Cases & Covers', '82283343~Electronics~Mobile Cases & Covers']
#100000 lists
x = x * 100000
#create new df with column _id
df = pd.DataFrame({'_id': x })
print df.head()
_id
0 82283344~Electronics~Mobile Cases & Covers
1 82283346~Electronics~Mobile Cases & Covers
2 82283343~Electronics~Mobile Cases & Covers
3 82283344~Electronics~Mobile Cases & Covers
4 82283346~Electronics~Mobile Cases & Covers
def DF(df):
df[['one', 'two', 'three']] = pd.DataFrame([ x.split('~') for x in df['_id'].tolist() ])
def AP(df):
df['one'] = df._id.apply(lambda x: x.split('~')[0])
df['two'] = df._id.apply(lambda x: x.split('~')[1])
df['three'] = df._id.apply(lambda x: x.split('~')[2])
def EX(df):
df[['one', 'two', 'three']] = df._id.str.split('~', expand=True)
def SP(df):
df['one'] = df['_id'].str.split('~').str[0]
df['two'] = df['_id'].str.split('~').str[1]
df['three'] = df['_id'].str.split('~').str[2]
DF(df)
print df.head()
AP(df)
print df.head()
EX(df)
print df.head()
SP(df)
print df.head()
重复4次:
_id one two \
0 82283344~Electronics~Mobile Cases & Covers 82283344 Electronics
1 82283346~Electronics~Mobile Cases & Covers 82283346 Electronics
2 82283343~Electronics~Mobile Cases & Covers 82283343 Electronics
3 82283344~Electronics~Mobile Cases & Covers 82283344 Electronics
4 82283346~Electronics~Mobile Cases & Covers 82283346 Electronics
three
0 Mobile Cases & Covers
1 Mobile Cases & Covers
2 Mobile Cases & Covers
3 Mobile Cases & Covers
4 Mobile Cases & Covers
定时:
In [125]: %timeit DF(df)
...: %timeit AP(df)
...: %timeit EX(df)
...: %timeit SP(df)
...:
1 loops, best of 3: 332 ms per loop
1 loops, best of 3: 564 ms per loop
1 loops, best of 3: 668 ms per loop
1 loops, best of 3: 1.09 s per loop
答案 1 :(得分:0)
我认为将一列分为三列并再次保存到同一数据帧应该可以正常工作:
df = df['_id'].str.split('~', 3, expand=True)
请尝试此操作,如果您有任何问题,请告诉我们。