pandas 如何在Python中添加一个dataframe到另一个dataframe

xjreopfe  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(170)

所以我有3列。ETA(eta/km*100)(一个数字),气候和月份。
我的目的是为3个月和气候的每个子集删除高于0.95四分位数和低于0.05(该数据集上的极端情况)的值,并将数据集重新分组到单个数据集上。
我在这里遇到的问题是,即使在“for”语句中,它也完成了这项工作,当我打印结果 Dataframe 时,它只有最后一个子集(Hurricane,last 3 months),而没有丢弃极端数据。
我已经尝试了concat,add和append。不知道我在这里做错了什么。

Climate = ['Sunny', 'Cloudy', 'Foggy', 'Rain', 'Storm', 'Hurricane']
newDf = df_Cl
newDf.iloc[0:0]

for cl in Climate:
    print (cl)
    for num in range(4, 14, 3):
        print (num)
        df_Temp = df.loc[(df['Climate'] == cl) & (df['month'] < num)]
        bajo = df_Temp['eta/km*100'].quantile(0.05)
        alto = df_Temp['eta/km*100'].quantile(0.95)
        df_Temp = df_Temp[df_Temp['eta/km*100'] > bajo]
        df_Temp = df_Temp[df_Temp['eta/km*100'] < alto]
        newDf.add(df_Temp)

我也试过:

newDf += df_Temp

但是所有的值都变成了“NaN”

njthzxwz

njthzxwz1#

用途:

Climate = ['Sunny', 'Cloudy', 'Foggy', 'Rain', 'Storm', 'Hurricane']

#filter only rows by Climate list
df1 = df[df['Climate'].isin(Climate)]

#create groups per Climate and each 3 months
g = df1.groupby(['Climate', df['month'].sub(1).floordiv(3)])['eta/km*100']

#filter between 0.05 and 0.95 quantile
out = df1[df1['eta/km*100'].between(g.quantile(0.05),
                                    g.quantile.quantile(0.95), inclusive='neither')]

如果将final df_Temp附加到DataFrames的lsit中,并且最后使用concat连接在一起,则解决方案有效:

L = []
for cl in Climate:
    print (cl)
    for num in range(4, 14, 3):
        print (num)
        df_Temp = df.loc[(df['Climate'] == cl) & (df['month'] < num)]
        bajo = df_Temp['eta/km*100'].quantile(0.05)
        alto = df_Temp['eta/km*100'].quantile(0.95)
        df_Temp = df_Temp[df_Temp['eta/km*100'] > bajo]
        df_Temp = df_Temp[df_Temp['eta/km*100'] < alto]
        L.append(df_Temp)
        
out = pd.concat(L)

相关问题