可能是一个简单的问题,我已经搜索过但找不到解决方案。
我的代码是这样的
data_df = pd.DataFrame([
['2012-02-22', '3', 'a', 6],
['2012-02-23', '3.2', 'g', 8],
['2012-02-24', '5.2', 'l', 2],
['2012-02-25', '1.4', 'i', 4]],
columns=['date', '1', '2', '3'])
dict_a = {
'a': np.array([False, True, False, False], dtype='bool'),
'b': np.array([True, True, False, False], dtype='bool'),
'c': np.array([False, True, True, False], dtype='bool'),
}
我想要一个像这样的 df
1 2 3 a b c
date
2012-02-22 3 a 6 False True False
2012-02-23 3.2 g 8 True True True
2012-02-24 5.2 l 2 False False True
2012-02-25 1.4 i 4 False False False
到目前为止,我发现的最好的方法就是这个,但对我来说这似乎很老套
data_df = data_df.set_index('date')
df_dict = pd.DataFrame.from_dict(dict_a)
df_dict['date'] = data_df.index
df_dict = df_dict.set_index('date')
df_new = pd.merge(data_df, df_dict, left_index=True, right_index=True)
有更快/更好的方法来实现它吗?
斯蒂芬大帝
精慕HU
茅侃侃
富国沪深
相关分类