pandas ValueError: 您正在尝试合并 datetime64[ns] 和 object 列。如果你想继续,你应该使用 pd.concat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52108703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:59:22  来源:igfitidea点击:

ValueError: You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat

pythonpandas

提问by user10102827

I want to make a pivot table and fill to it in Date.I wrote codes.

我想制作一个数据透视表并在日期中填充它。我写了代码。

user_dct ={100:"Tom",101:"Jon",102:"Daisy"}
for key,value in user_dct.items():
         file= './'+value+'.csv'
       df = pd.read_csv(file)

Each df is

每个 df 是

    Date        ID        Name    Score        Rank
0    2011-01-12  100     Tom        40            C
1    2011-01-14  100     Tom        60            B
2    2011-01-19  100     Tom        80            A
?
?
?


   Date        ID        Name    Score        Rank
0    2011-01-12  101     Jon        30            C
1    2011-01-14  101     Jon        50            C
2    2011-01-19  101     Jon        60            B
?
?
?

user_dct ={100:"Tom",101:"Jon",102:"Daisy"}
dfs = []
for key,value in user_dct.items():
    file= './'+value+'.csv'
    dfs.append(pd.read_csv(file, parse_dates=['Date']))

df = pd.concat(dfs, ignore_index=True)
df =df.sort_values(['Date','ID']).set_index(['Date','ID'])

date_df = pd.DataFrame({'Date':pd.date_range('2011-01-01','2011-12-31',freq='1D').strftime('%Y-%m-%d')})
df = pd.merge(df, date_df, on='Date', how='outer').fillna(0)

My ideal output is

我的理想输出是

                        Name    Score        Rank
    Date      ID  
2011-01-01  100         Tom       0              0
                    101         Jon        0              0
                    102     Daisy       0              0  
                    ?
                                         ?
                                         ?
2011-01-12  100         Tom       40            C
                    101         Jon        30            C
                    102     Daisy       90            S
2011-01-14  100     Tom         60            B
                    101     Jon           50            C
                     102     Daisy       90            S
2011-01-19  100     Tom         80            A
                     101     Jon          60            B
                     102     Daisy      80            A
?
?
?

What is wrong in my codes?How should I fix this?Why does int type error happens?I changed sort_values&set_index ,but error is not disappeared.

我的代码有什么问题?我应该如何解决这个问题?为什么会发生 int 类型错误?我更改了 sort_values&set_index ,但错误并未消失。

采纳答案by jezrael

I think need:

我认为需要:

idx = pd.date_range('2011-01-01','2011-12-31')
mux = pd.MultiIndex.from_product([idx, df['ID'].unique()], names=['Date','ID'])
df = df.set_index(['Date','ID']).reindex(mux, fill_value=0)
print (df.head())
               Name  Score Rank
Date       ID                  
2011-01-01 100    0      0    0
           101    0      0    0
2011-01-02 100    0      0    0
           101    0      0    0
2011-01-03 100    0      0    0