合并数据帧混乱

时间:2016-06-24 10:30:33

标签: python pandas

我对合并几个数据帧感到有些困惑。调用以下数据帧(dayData和data):

dayData:

CREATE FUNCTION CheckStringOfUpperAlphaOK(@String varchar(MAX))
Returns VarChar(6)
AS
Begin

    Declare @KeepValues as varchar(50)
    Set @KeepValues = '%[^ ][A-Z]%'
    While PatIndex(@KeepValues collate Latin1_General_Bin, @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex(@KeepValues collate Latin1_General_Bin, @Temp) + 1, 0, ' ')

    Return @Temp
End

数据:

               id          stock_name    broad_sector currency
0  BBG.XLON.BTA.S        BT GROUP PLC  Communications      GBp
1  BBG.XLON.VOD.S  VODAFONE GROUP PLC  Communications      GBp

dayData type <class 'pandas.core.series.Series'>
dayData index  [0 1]

dayData数据帧是一个系列,所以我转换为数据帧,然后尝试合并datae数据帧:

            id
BBG.XLON.BTA.S    301.221525
BBG.XLON.VOD.S    213.791400

Name: 2008-02-20 00:00:00, dtype: float64
data index  [0 1]

但是返回的dayData数据帧为空:

data = data.to_frame().reset_index()
data.columns = ['id', 'price']
dayData = dayData.merge(data[['price']], left_on='id', right_index=True)

我希望返回的是:

Empty DataFrame
Columns: [id, stock_name, broad_sector, currency, price]
Index: []

知道我做错了吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

我认为你可以join SerieDataFrame,但在Serie name price data.name = 'price'之前设置print (dayData) id stock_name broad_sector currency 0 BBG.XLON.BTA.S BT GROUP PLC Communications GBp 1 BBG.XLON.VOD.S VODAFONE GROUP PLC Communications GBp data.name = 'price' print (data) id BBG.XLON.BTA.S 301.221525 BBG.XLON.VOD.S 213.791400 Name: price, dtype: float64 dayData = dayData.join(data, on='id') print (dayData) id stock_name broad_sector currency price 0 BBG.XLON.BTA.S BT GROUP PLC Communications GBp 301.221525 1 BBG.XLON.VOD.S VODAFONE GROUP PLC Communications GBp 213.791400

public class Account {

        @SerializedName("uid")
        @Expose
        private String uid;
        @SerializedName("name")
        @Expose
        private String name;

        /**
         * @return The uid
         */
        public String getUid() {
            return uid;
        }

        /**
         * @param uid The uid
         */
        public void setUid(String uid) {
            this.uid = uid;
        }

        /**
         * @return The name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name The name
         */
        public void setName(String name) {
            this.name = name;
        }

}

Joining key columns on an index