因此,我已经在这方面工作了一段时间,但没有任何地方,也不确定什么是do.Fairly新的熊猫和蟒蛇。
数据集实际上是15,000个产品名称。它们都有不同的格式,一些有多个破折号,最多6个,一些连字符,不同的长度,行是带有变体的产品名称。
当我在大型数据集上使用它时,我使用的代码只返回第一个字母,而不是部分字符串。
在我用来测试它的一个小数据集上工作得很好。
我假设这是因为:
在大数据集上克服这个问题的最好方法是什么,我缺少什么?或者我必须要做这本手册?
原始测试数据集
`1.star t-shirt-large-red
2.star t-shirt-large-blue
3.star t-shirt-small-red
4.beautiful rainbow skirt small
5.long maxwell logan jeans- light blue -32L-28W
6.long maxwell logan jeans- Dark blue -32L-28W`
所需的数据集/输出:
`COL1 COL2 COL3 COL4
1[star t-shirt] [large] [red] NONE
2[star t-shirt] [large] [blue] NONE
3[star t-shirt] [small] [red] NONE
4[beautiful rainbow skirt small] [small] NONE NONE
5[long maxwell logan jeans] [light blue] [32L] [28W]
6[long maxwell logan jeans] [Dark blue] [32L] [28W]`
这是我在上一个问题中得到帮助的代码:
`df['onkey'] = 1
df1 = pd.merge(df[['name','onkey']],df[['name','onkey']], on='onkey')
df1['list'] = df1.apply(lambda x:[x.name_x,x.name_y],axis=1)
from os.path import commonprefix
df1['COL1'] = df1['list'].apply(lambda x:commonprefix(x))
df1['COL1_num'] = df1['COL1'].apply(lambda x:len(x))
df1 = df1[(df1['COL1_num']!=0)]
df1 = df1.loc[df1.groupby('name_x')['COL1_num'].idxmin()]
df = df.rename(columns ={'name':'name_x'})
df = pd.merge(df,df1[['name_x','COL1']],on='name_x',how ='left')`
`df['len'] = df['COL1'].apply(lambda x: len(x))
df['other'] = df.apply(lambda x: x.name_x[x.len:],axis=1)
df['COL1'] = df['COL1'].apply(lambda x: x.strip())
df['COL1'] = df['COL1'].apply(lambda x: x[:-1] if x[-1]=='-' else x)
df['other'] = df['other'].apply(lambda x:x.split('-'))
df = df[['COL1','other']]
df = pd.concat([df['COL1'],df['other'].apply(pd.Series)],axis=1)`
` COL1 0 1 2
0 star t-shirt large red NaN
1 star t-shirt large blue NaN
2 star t-shirt small red NaN
3 beautiful rainbow skirt small NaN NaN
4 long maxwell logan jeans light blue 32L 28W
5 long maxwell logan jeans Dark blue 32L 28W`
*更新*
1. If the product /string is unique just print into the column with the extracted longest string.
star t-shirt-large-red star t-shirt-large-blue star t-shirt-small-red beautiful rainbow skirt small long maxwell logan jeans- light blue -32L-28W long maxwell logan jeans- Dark blue -32L-28W Organic and natural candy - 3 Pack - Mint Organic and natural candy - 3 Pack - Vanilla Organic and natural candy - 3 Pack - Strawberry Organic and natural candy - 3 Pack - Chocolate Organic and natural candy - 3 Pack - Banana Organic and natural candy - 3 Pack - Cola Organic and natural candy - 12 Pack Assorted Morgan T-shirt Company - Small/Medium-Blue Morgan T-shirt Company - Medium/Large-Blue Morgan T-shirt Company - Medium/Large-red Morgan T-shirt Company - Small/Medium-Red Morgan T-shirt Company - Small/Medium-Green Morgan T-shirt Company - Medium/Large-Green Nelly dress leopard small
col1 col2 col3 col4 star t-shirt large red
star t-shirt large blue
star t-shirt small red
beautiful rainbow skirt small
Long maxwell logan jeans light blue 32L 28W Long maxwell logan jeans Dark blue 32L 28W Organic and natural candy 3 Pack Mint Organic and natural candy 3 Pack Vanilla
Organic and natural candy 3 Pack Strawberry
Organic and natural candy 3 Pack Chocolate
Organic and natural candy 3 Pack Banana
Organic and natural candy 3 Pack Cola Organic and natural candy 12 Pack Assorted Morgan T-shirt Company Small/Medium Blue Morgan T-shirt Company Medium/Large Blue Morgan T-shirt Company Medium/Large Red
Morgan T-shirt Company Small/Medium Red
Morgan T-shirt Company Small/Medium Green
Morgan T-shirt Company Medium/Large Green
Nelly dress Leopard Small
Bijoux
Princess PJ-set
Lemon tank top Yellow Medium
发布于 2018-09-11 08:29:15
构建DataFrame df,如下所示:
df = pd.DataFrame()
df = df.append(['1.star t-shirt-large-red'])
df = df.append(['2.star t-shirt-large-blue'])
df = df.append(['4.beautiful rainbow skirt small'])
df = df.append(['5.long maxwell logan jeans- light blue -32L-28W'])
df = df.append(['6.long maxwell logan jeans- Dark blue -32L-28W'])
df.columns = ['Product']
下面的代码
(a)去掉任何空格,
(b)以句点(‘’)拆分然后抓住下面的东西,
(c)将“t恤”替换为“t恤”,因为需要进一步的操作(如果在操作后需要,请将其改回原处)
(d)再次以'-‘拆分,并扩展以提供数据帧。
df['Product'].str.strip().str.split('.').str.get(1).str.replace('t-shirt', 'tshirt').str.split('-', expand = True)
输出:
0 1 2 3
0 star tshirt large red None
0 star tshirt large blue None
0 beautiful rainbow skirt small None None None
0 long maxwell logan jeans light blue 32L 28W
0 long maxwell logan jeans Dark blue 32L 28W
考虑到您的产品在命名上的不一致,将会遗漏一些边缘案例(例如:beautiful rainbow skirt small
)。你可能还得再把它们钓出来。
发布于 2018-09-11 09:21:43
下面是一个非常容易理解、调试和灵活扩展的解决方案:
假设您的初始产品名称保存在一个名为strings
的列表中。
然后,解决方案是下面这一行:
mydf = pd.concat([pd.DataFrame([make_row(row, 4)], columns=['COL1', 'COL2', 'COL3', 'COL4']) for row in strings], ignore_index=True)
其中,我们已经将解析函数make_row
定义为:
def make_row(string, num_cols):
cols = [item.strip() for item in string[2:].split('-')] # ignore numbering, split on hyphen and strip whitespace
if len(cols) < num_cols:
cols += [np.nan]*(num_cols - len(cols)) # fill with NaN missing values
return cols
定义cols
的第一行也可以是简单的cols = string.split('-')
,在这种情况下,您可以在以后使用以下命令进行格式化:
mydf.applymap(lambda x: x if pd.isnull(x) else str.strip(x))
现在,在您的例子中,我看到您的一些产品名称中有一个连字符,在这种情况下,您可能希望提前“清理”它们(或在make_row
中,如您所愿),如下所示:
strings = [item.replace('t-shirt', 'tshirt') for item in strings]
示例输入
strings = ['1.one-two-three', '2. one-two', '3.one-two-three-four', '4.one - two -three -four ']
输出
COL1 COL2 COL3 COL4
0 one two three NaN
1 one two NaN NaN
2 one two three four
3 one two three four
问题数据的输出(更正项目4的拼写错误后):
COL1 COL2 COL3 COL4
0 star tshirt large red NaN
1 star tshirt large blue NaN
2 star tshirt small red NaN
3 beautiful rainbow skirt small NaN NaN
4 long maxwell logan jeans light blue 32L 28W
5 long maxwell logan jeans Dark blue 32L 28W
编辑:
如果您还想将这些项“分组”在一起,那么您可以:
a)在如上所述获得数据帧后,在列COL1上使用sort_values
(pandas doc),以简单地逐个显示同一产品对应的行,或者
b)使用group_by
实际获取分组数据帧,如下所示:
grouped_df = mydf.groupby("COL1")
这将允许您像这样获取每个组:
grouped_df.get_group("star tshirt")
生成以下输出:
COL1 COL2 COL3 COL4
0 star tshirt large red NaN
1 star tshirt large blue NaN
2 star tshirt small red NaN
https://stackoverflow.com/questions/52267034
复制相似问题