简体   繁体   中英

python pandas dataframe filling e.g. bfill, ffill

I have two problems with filling out a very large dataframe. There is a section of the picture. I want the 1000 in E and F to be pulled down to 26 and no further. In the same way I want the 2000 to be pulled up to -1 and down to the next 26. I thought I could do this with bfill and ffill, but unfortunately I don't know how...(picture1) 在此处输入图像描述

Another problem is that columns occur in which the values from -1 to 26 do not contain any values in E and F. How can I delete or fill them with 0 so that no bfill or ffill makes wrong entries there? (picture2) 在此处输入图像描述

import pandas as pd
import numpy as np

data = '/Users/Hanna/Desktop/Coding/Code.csv'


df_1 = pd.read_csv(data,usecols=["A",
                           "B",
                           "C",
                           "D",
                           "E",
                           "F",
                           ],nrows=75)


base_list =[-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
df_c = pd.MultiIndex.from_product([
[4000074],
["SP000796746","SP001811642"],
[201824, 201828, 201832, 201835, 201837, 201839, 201845, 201850, 201910, 201918, 201922, 201926, 201909, 201916, 201918, 201920],
base_list],

names=["A", "B", "C", "D"]).to_frame(index=False)
df_3 = pd.merge(df_c, df_1, how='outer')

To understand it better, I have shortened the example a bit. Picture 3 shows how it looks like when it is filled and picture 4 shows it correctly filled在此处输入图像描述

在此处输入图像描述

could find the indexes where you have -1 and then slice/loop over the columns to fill.

just to create the sample data:

import pandas as pd
df = pd.DataFrame(columns=list('ABE'))
df['A']=list(range(-1, 26)) * 10

add random values at each section

import random 

for i in df.index:
    if i%27 == 0:
        df.loc[i,'B'] = random.random()
    else:
        df.loc[i, 'B'] = 0

find the indexes to slice over

indx = df[df['A'] == -1].index.values

fill out data in column "E"

for i, j in zip(indx[:-1], indx[1:]):
    df.loc[i:j-1, 'E'] = df.loc[i:j-1, 'B'].max()

    if j == indx[-1]:
        df.loc[j:, 'E'] = df.loc[j:, 'B'].max()

Assuming you have to find and fill values for a particular segment.

data = pd.read_csv('/Users/Hanna/Desktop/Coding/Code.csv')    
for i in range(0,data.shape[0],27):
        if i+27 < data.shape[0]:
            data.loc[i:i+27,'E'] = max(data['E'].iloc[i:i+27])
        else:
            data.loc[i:data.shape[0],'E'] = max(data['E'].iloc[i:data.shape[0]])

you can replace the max to whatever you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM