下記のように複数の同じindex で構成されたデータフレームがあります。
このデータフレームを一つのデータフレームとして出力する方法を教えてください。
python
1import pandas as pd 2frame1 = pd.DataFrame( 3 {'name':['chika','you','riko','yoshiko','ruby']}, 4 ,columns=[name],index=[2014,2033,2211,3400,3566] 5 ) 6print(frame1) 7#frame1: 8#number name 9#2014 chika 10#2033 you 11#2211 riko 12#3400 yoshiko 13#3566 ruby 14 15frame2 = pd.DataFrame( 16 {'birthday':['0801','0417','0919','0713','0921']}, 17 ,columns=[birthday],index=[2014,2033,2211,3400,3566] 18 ) 19print(frame2) 20#frame2: 21#number birthday 22#2014 0801 23#2033 0417 24#2211 0919 25#3400 0713 26#3566 0921 27 28#上記のframe1とframe2結合して下記のように結果を出したいと思っております。 29 30#number name birthday 31#2014 chika 0801 32#2033 you 0417 33#2211 riko 0919 34#3400 yoshiko 0713 35#3566 ruby 0921
上記の例では下記のように示すと結果を得ることができました。
python
1import pandas as pd 2frame1 = pd.DataFrame({'name':['chika','you','riko','yoshiko','ruby']},index=[2014,2033,2211,3400,3566]) 3frame2 = pd.DataFrame({'birthday':['0801','0417','0919','0713','0921']},index=[2014,2033,2211,3400,3566]) 4ret = frame1.merge(frame2,left_index=True,right_index=True) 5print(ret) 6""" 7 name birthday 82014 chika 0801 92033 you 0417 102211 riko 0919 113400 yoshiko 0713 123566 ruby 0921 13"""
そこでデータフレームの中を下記のように変えたところ、
AttributeError: 'Series' object has no attribute 'merge'
エラーが返ってきました。
python
1#frame1 2#number 3#2014 5.894737 4#2538 7.214286 5#2698 5.583333 6#2785 7.000000 7#2787 7.764706 8#2816 5.722222 9#2820 0.000000 10# ・・・ 11#Length: 1606, dtype: float64 12 13#frame2 14#number 15#2014 5.894737 16#2538 7.214286 17#2698 5.583333 18#2785 7.000000 19#2787 7.764706 20#2816 5.722222 21#2820 0.000000 22# ・・・ 23#Length: 1606, dtype: float64 24import pandas as pd 25ret = frame1.merge(frame2,left_index=True,right_index=True) 26 27#結果 28#AttributeError: 'Series' object has no attribute 'merge'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/24 04:43
2018/11/24 04:46
2018/11/24 05:04
2018/11/24 05:07
2018/11/25 00:02
2018/11/25 00:16