Slice Pandas series (based on index) into multiple columns of dataframe

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

As an example, I'm trying to slice a field, 'date', containing dates in the format YYYYMMDD into 3 individual fields ('year', 'month', 'day').

I have an approach that assigns each value one at a time, but I assume there is a more efficient way to produce the desired result.

Current solution:

df['year'] = df['date'].astype(str).apply(lambda x: x[:4]) df['month'] = df['date'].astype(str).apply(lambda x: x[4:6]) df['day'] = df['date'].astype(str).apply(lambda x: x[6:8])

The following is an example of one of my attempts to simplify the code:

df['year'], df['month'], df['day'] = df['date'].astype(str).apply(lambda x: [x[:4], x[4:6], x[6:8]])

asked 21 secs ago
Billy Bob

نویسنده : استخدام کار بازدید : 7 تاريخ : سه شنبه 22 خرداد 1397 ساعت: 3:40

فهرست وبلاگ