我有数据框df:
df = pd.DataFrame({'col1':[1,5,7,9],'col2':['abc','defg','fghei','swe']})
df.index = ['1234','1235','1236','1237']
df
col1 col2
1234 1 abc
1235 5 defg
1236 7 fghei
1237 9 swe
现在,我想在col2
的每个值之前附加索引,但是列值的长度不应大于6。如果大于7,则应除去索引。
必填输出:
col1 col2
1234 1 1234abc
1235 5 123defg
1236 7 12fghei
1237 9 1234swe
我尝试过的事情:
df['col2_len'] = df['col2'].apply(lambda x: len(x))
df['idx'] = df.index
df['idx_len'] = df['idx'].apply(lambda x: len(x))
df['col2'] = df['idx'] + df['col2']
print(df)
df['col2'] = df['col2'].apply(lambda x: x[0:7])
df.drop(['col2_len','idx','idx_len'],1)
哪个给我以下输出:
col1 col2
1234 1 1234abc
1235 5 1235def
1236 7 1236fgh
1237 9 1237swe
答案 0 :(得分:0)
使用list comprehension
:
df['col2'] = [x[: 7-len(y)] + y for x, y in zip(df.index, df['col2'])]
print (df)
col1 col2
1234 1 1234abc
1235 5 123defg
1236 7 12fghei
1237 9 1237swe
说明:
zip
index
和tuples
的列值y
的每个值column
,长度要加len
7
用于切片值x
(index
)y