背景
我有下面的示例df
import pandas as pd
df = pd.DataFrame({'Before' : [['there, are, many, different'],
['i, like, a, lot, of, sports '],
['the, middle, east, has, many']],
'After' : [['in, the, bright, blue, box'],
['because, they, go, really, fast'],
['to, ride, and, have, fun'] ],
'P_ID': [1,2,3],
'Word' : ['crayons', 'cars', 'camels'],
'N_ID' : ['A1', 'A2', 'A3']
})输出
After Before N_ID P_ID Word
0 [in, the, bright, blue, box] [there, are, many, different] A1 1 crayons
1 [because, they, go, really,fast] [i, like, a, lot, of, sports ] A2 2 cars
2 [to, ride, and, have, fun] [the, middle, east, has, many] A3 3 camels期望输出
After Before N_ID P_ID Word
0 in the bright blue box there are many different A1 1 crayons
1 because they go really fast i like a lot of sports A2 2 cars
2 to ride and have fun the middle east has many A3 3 camels问题
如何获得我想要的输出,即1)、未列出和2)已删除逗号?
发布于 2019-07-06 01:51:11
正如你所证实的,解决办法很简单。一栏:
df.After.str[0].str.replace(',', '')
Out[2821]:
0 in the bright blue box
1 because they go really fast
2 to ride and have fun
Name: After, dtype: object对于具有列表的所有列,您需要使用apply并按如下方式重新分配:
df.loc[:, ['After', 'Before']] = df[['After', 'Before']].apply(lambda x: x.str[0].str.replace(',', ''))
Out[2824]:
After Before N_ID P_ID Word
0 in the bright blue box there are many different A1 1 crayons
1 because they go really fast i like a lot of sports A2 2 cars
2 to ride and have fun the middle east has many A3 3 camelshttps://stackoverflow.com/questions/56910537
复制相似问题