請檢查下面代碼中的注釋文本以了解問題。
import pandas as pd
import numpy as np
primary = pd.DataFrame(
data = ['little','mary','had','a','swan'],
index =pd.DatetimeIndex(['2015-09-25 12:00:00',
'2015-09-25 13:00:00',
'2015-09-25 14:00:00',
'2015-09-25 15:00:00',
'2015-09-25 16:00:00']),
columns=['some_nonsense'])
secondary = pd.DataFrame(
data = ['mommy',np.nan],
index =pd.DatetimeIndex(['2015-09-25 14:00:00',
'2015-09-25 15:00:00']),
columns=['copy_me'])
# 1. secondary dataframe values have already been computed
# 2. we want to assign them to the primary dataframe for available dates
# 3. once done, we want to return dataframe index locations for missing values
# 4. nan is one of the valid values the secondary dataframe can take
primary['copy_me'] = secondary['copy_me']
print (secondary)
print (primary)
# The values have been copied successfully
# But how to get the locations of missing indices?
# The expected result is as follows:
# If I know these values I could pass them to my computing function
missing_indices = np.array([0,1,4])
print('needed result: ', missing_indices)
uj5u.com熱心網友回復:
如果我理解正確,這可能會有所幫助:
(~primary.index.isin(secondary.index)).nonzero()[0]
分解:
- 找出( )
primary中存在哪些索引。secondaryprimary.index.isin(secondary.index) - 否定那個條件 (
~)。 - 使用查找值非零的位置,
True即numpy.nonzero。(.nonzero()[0],[0]因為它回傳一個元組)
uj5u.com熱心網友回復:
您可以檢查是否primary.index在secondary.index:
np.flatnonzero(~primary.index.isin(secondary.index))
# array([0, 1, 4], dtype=int32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429195.html
標籤:python-3.x 熊猫 麻木的
