如何基于另一列切片数据框列

时间:2018-05-08 11:16:00

标签: python pandas dataframe data-analysis

我有这样的df,

Main                        Length
Sri playnig well cricket    5
sri went out                2
Ram is in                   1 
Ram went to UK,US           2

我正在尝试切片df["Main"] based on df["Length"]

我的预期输出是,

Main                        Length
Sri p                       5
sr                          2
R                           1 
Ra                          2

我试过

def slicer(row):
    for i in df["Length"]:
        row['Main'].slice(0,i)
    return row

 df.apply(slicer,axis=1)

但我得到了,AttributeError: ("'str' object has no attribute 'slice'", 'occurred at index 0') 请帮忙。

1 个答案:

答案 0 :(得分:3)

使用apply编制索引:

df['Main'] = df.apply(lambda x: x['Main'][:x['Length']], axis=1)

如果没有zip的值,则使用NaN列出理解:

df['Main'] = [a[:b] for a, b in zip(df['Main'], df['Length'])]
print(df)
    Main  Length
0  Sri p       5
1     sr       2
2      R       1
3     Ra       2

对于更一般的解决方案,可以使用if-else

df['Main'] = [a[:b] if len(a) < b else a for a, b in zip(df['Main'], df['Length'])]
print(df)
                       Main  Length
0  Sri playnig well cricket     100
1              sri went out       2
2                 Ram is in       1
3         Ram went to UK,US       2