1
나는 다음과 같은 데이터 프레임 my_df
에게 있습니다팬더가 : 여러 열에서 튜플 열을 생성
Person event time
---------------------------------
John A 2017-10-11
John B 2017-10-12
John C 2017-10-14
John D 2017-10-15
Ann X 2017-09-01
Ann Y 2017-09-02
Dave M 2017-10-05
Dave N 2017-10-07
Dave Q 2017-10-20
. 그것은과 같아야합니다
여기Person event time event_time
------------------------------------------------------
John A 2017-10-11 (A, 2017-10-11)
John B 2017-10-12 (B, 2017-10-12)
John C 2017-10-14 (C, 2017-10-14)
John D 2017-10-15 (D, 2017-10-15)
Ann X 2017-09-01 (X, 2017-09-01)
Ann Y 2017-09-02 (Y, 2017-09-02)
Dave M 2017-10-05 (M, 2017-10-05)
Dave N 2017-10-07 (N, 2017-10-07)
Dave Q 2017-10-20 (Q, 2017-10-20)
내 코드 :
my_df['event_time'] = my_df.apply(lambda row: (row['event'] , row['time']), axis=1)
하지만 다음과 같은 오류 있어요 : 내 코드에서 뭘 잘못했는지
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in create_block_manager_from_arrays(arrays, names, axes)
4309 blocks = form_blocks(arrays, names, axes)
-> 4310 mgr = BlockManager(blocks, axes)
4311 mgr._consolidate_inplace()
/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in __init__(self, blocks, axes, do_integrity_check, fastpath)
2794 if do_integrity_check:
-> 2795 self._verify_integrity()
2796
/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in _verify_integrity(self)
3005 if block._verify_integrity and block.shape[1:] != mgr_shape[1:]:
-> 3006 construction_error(tot_items, block.shape[1:], self.axes)
3007 if len(self.items) != tot_items:
/usr/local/lib/python3.4/dist-packages/pandas/core/internals.py in construction_error(tot_items, block_shape, axes, e)
4279 raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 4280 passed, implied))
4281
ValueError: Shape of passed values is (128, 2), indices imply (128, 3)
어떤 생각을? 감사!
심
apply
나는 첫 번째 방법을 시도했지만 오류가있어 : 에 ValueError를 : 항목의 수가 잘못이 배치 1 내가 여기서 뭘 그리워 않았다 의미한다, (2)를 통과? – Edamame일부 NaN이 가능합니까? 나는 그것을 시험 할 것이다. – jezrael
예, 일부 이벤트가 '없음'으로 표시되지만 여전히 타임 스탬프가 있습니다. 해당 튜플이 (없음, 타임 스탬프) – Edamame