Python Pandas以递增方式填充NaN,其中包含上面单元格的单元格值

时间:2017-12-20 13:34:10

标签: python pandas dataframe nan

我有一个数据框如下......

           Word  Count  Team Sex    Code
0       develop      9   1     M  P45.01
1     Effective      7   NaN   M     NaN
2  professional      8   NaN   M     NaN
3      approach      5   NaN   M     NaN
4        raster     34   NaN   M     NaN
5           Sad     55   NaN   M     NaN
6         water      2   NaN   M     NaN
7          soil      7   NaN   M     NaN
8       farming      9   NaN   M     NaN
9          deep     12   NaN   M     NaN

我希望在“团队”列中填写NaN,1, 2, 3, 4, 5, ....递增,并且对“”代码“列填充相同,如下所示:P45.01, P46.01, P47.01, P48.01, ...。请参阅下面的最终数据框......

注意:团队数字,而代码是字符串列类型

enter image description here

df['Team'].fillna(df['Team'][0]+1)
df.fillna(method='ffill')

3 个答案:

答案 0 :(得分:3)

我相信您可以创建范围并将其添加到第一个值,Sex使用ffill

r = np.arange(len(df))
df['Team'] = df.loc[0, 'Team'] + r
df['Code'] = df.loc[0, 'Code'] + r
df['Sex'] = df['Sex'].ffill()
print (df)
           Word  Count  Team Sex   Code
0       develop      9   1.0   M  45.01
1     Effective      7   2.0   M  46.01
2  professional      8   3.0   M  47.01
3      approach      5   4.0   M  48.01
4        raster     34   5.0   M  49.01
5           Sad     55   6.0   M  50.01
6         water      2   7.0   M  51.01
7          soil      7   8.0   M  52.01
8       farming      9   9.0   M  53.01
9          deep     12  10.0   M  54.01

更广泛的广播:

cols = ['Team','Code']
r = np.arange(len(df))
df[cols] = r[:, None] + df.loc[0, cols].values
df['Sex'] = df['Sex'].ffill()
print (df)
           Word  Count  Team Sex   Code
0       develop      9   1.0   M  45.01
1     Effective      7   2.0   M  46.01
2  professional      8   3.0   M  47.01
3      approach      5   4.0   M  48.01
4        raster     34   5.0   M  49.01
5           Sad     55   6.0   M  50.01
6         water      2   7.0   M  51.01
7          soil      7   8.0   M  52.01
8       farming      9   9.0   M  53.01
9          deep     12  10.0   M  54.01

编辑:

如果在浮动之前只有字符串,则可以将其提取到df1,添加范围和最后添加前缀:

r = np.arange(len(df))
df['Team'] = (df.loc[0, 'Team'] + r).astype(int)
df1 = df.loc[[0], 'Code'].str.extract('(\D+)(\d+\.\d+)', expand=False)
print (df1)
   0      1
0  P  45.01

df['Code'] = float(df1.loc[0, 1]) + r
df['Code'] = df1.loc[0, 0] + df['Code'].astype(str)
df['Sex'] = df['Sex'].ffill()
print (df)

           Word  Count  Team Sex    Code
0       develop      9     1   M  P45.01
1     Effective      7     2   M  P46.01
2  professional      8     3   M  P47.01
3      approach      5     4   M  P48.01
4        raster     34     5   M  P49.01
5           Sad     55     6   M  P50.01
6         water      2     7   M  P51.01
7          soil      7     8   M  P52.01
8       farming      9     9   M  P53.01
9          deep     12    10   M  P54.01

编辑:

错误似乎没有第一个索引值0,而是其他内容。因此可以使用iloc按位置选择:

r = np.arange(len(df))
df['Team'] = (df.iloc[0, df.columns.get_loc('Team')] + r).astype(int)
df1 = df.iloc[[0], df.columns.get_loc('Code')].str.extract('(\D+)(\d+\.\d+)', expand=False)

df['Code'] = float(df1.loc[0, 1]) + r
df['Code'] = df1.loc[0, 0] + df['Code'].astype(str)
df['Sex'] = df['Sex'].ffill()
print (df)

           Word  Count  Team Sex    Code
0       develop      9     1   M  P45.01
1     Effective      7     2   M  P46.01
2  professional      8     3   M  P47.01
3      approach      5     4   M  P48.01
4        raster     34     5   M  P49.01
5           Sad     55     6   M  P50.01
6         water      2     7   M  P51.01
7          soil      7     8   M  P52.01
8       farming      9     9   M  P53.01
9          deep     12    10   M  P54.01

答案 1 :(得分:1)

df.insert(0,'Team',range(1,1+len(df)))

这适用于现有的1和2

df=df.assign(Team=[i for i in xrange(len(df))])[['Team']+df.columns.tolist()]

或最终

df=df.reset_index()
df['Team']=df.index+1

没有对它进行测试,但它应该可行

答案 2 :(得分:0)

我意识到您也可以执行以下操作之一,在此处更新dataframe

设置

import re

def count(n):
    x = 0
    while x < n:
        yield x
        x += 1

def populate(s, step):
    chars = re.split('(\d*)', s)
    number = int(chars[1]) + step
    chars[1] = str(number)
    return ''.join(chars)

number_generator = count(10)
number_generator2 = count(10)

主要

# option 1 for `team` column
df['Team'] = df.index + 1 # just use your indices.

# option 2 for 'team' column
df.loc[:, 'Team'] = df.Team.apply(lambda _: next(number_generator)) # use generator.

# output

           Word  Count  Team Sex    Code
0       develop      9     1   M  P45.01
1     Effective      7     2   M     NaN
2  professional      8     3   M     NaN
3      approach      5     4   M     NaN
4        raster     34     5   M     NaN
5           Sad     55     6   M     NaN
6         water      2     7   M     NaN
7          soil      7     8   M     NaN
8       farming      9     9   M     NaN
9          deep     12    10   M     NaN

# we can use generator again for `Code` column
df.loc[:, 'Code']df.Code.apply(lambda _: populate(df.loc[0, 'Code'], next(number_generator2)))

# output 
           Word  Count  Team Sex    Code
0       develop      9     1   M  P45.01
1     Effective      7     2   M  P46.01
2  professional      8     3   M  P47.01
3      approach      5     4   M  P48.01
4        raster     34     5   M  P49.01
5           Sad     55     6   M  P50.01
6         water      2     7   M  P51.01
7          soil      7     8   M  P52.01
8       farming      9     9   M  P53.01
9          deep     12    10   M  P54.01

我们需要注意的一件事是,发电机正在生成数字,只能使用一次;但它完成了这项工作。