Pandas将变量添加为列并对应于原始表(但它们具有不同的长度)

时间:2017-03-28 12:16:36

标签: python pandas

我要问的是标题有点复杂。我做了这个例子来向你展示我的问题。以下是示例表:

df = pd.DataFrame({'Number': [1,2,3,4,5,6,7,8,9], 'Col1':['a','b','c','d','e','f','g','h','i']})

下一步是提取df ['Number']并出于某种原因运行迭代。 number= [i*i for i in df['Number']]输出为[1, 4, 9, 16, 25, 36, 49, 64, 81]

现在我有一个变量'number',这是一个列表。

现在关键的一步是我必须重新组合这个列表。假设数字小于40

number1 = [i for i in number if i < 40]
number2 = [i for i in number if i > 40]

好的,我想要的关键步骤是将number1和number2添加到df,但预期的最终输出是这样的:

也就是说,添加一个新列'Type',这两个新变量必须与索引匹配,内容为'number1'和'number2',但不是'1,4,9 ... 81'。

3 个答案:

答案 0 :(得分:3)

我认为boolean mask需要numpy.where

print (df.Number.pow(2) < 40)
0     True
1     True
2     True
3     True
4     True
5     True
6    False
7    False
8    False
Name: Number, dtype: bool

df['Type'] = np.where(df.Number.pow(2) < 40, 'number1', 'number2')
#same as
#df['Type'] = np.where(df.Number ** 2 < 40, 'number1', 'number2')
#another solution
#df['Type'] = np.where(df.Number.pow(2).lt(40), 'number1', 'number2')
print (df)
  Col1  Number     Type
0    a       1  number1
1    b       2  number1
2    c       3  number1
3    d       4  number1
4    e       5  number1
5    f       6  number1
6    g       7  number2
7    h       8  number2
8    i       9  number2

计时 - numpy.where最快:

df = pd.DataFrame({'Number': [1,2,3,4,5,6,7,8,9], 'Col1':['a','b','c','d','e','f','g','h','i']})
#[9000 rows x 5 columns]
df = pd.concat([df]*1000).reset_index(drop=True)

df['Type'] = np.where(df.Number.pow(2).lt(40), 'number1', 'number2')

df['Type1'] = 'number' + (1 + ((df.Number**2)>40).astype(int)).astype(str)

# Rule to produce new values
def f(row):
    if row['Number']**2 > 40:
        val = 'Number2'
    else:
        val = 'Number1'
    return val

df['Type2'] = df.apply(f, axis=1)

#print (df)
In [218]: %timeit df['Type'] = np.where(df.Number.pow(2).lt(40), 'number1', 'number2')
1000 loops, best of 3: 1.63 ms per loop

In [219]: %timeit df['Type1'] = 'number' + (1 + ((df.Number**2)>40).astype(int)).astype(str)
100 loops, best of 3: 13.5 ms per loop

In [220]: %timeit df['Type2'] = df.apply(f, axis=1)
10 loops, best of 3: 127 ms per loop

编辑:

我创建帮助列以便更好地理解比较:

df['pow'] = df.Number.pow(2)
df['comaping val'] = 40
df['val'] = df.Number.pow(2).lt(40)
print (df)
  Col1  Number  pow  comaping val    val
0    a       1    1            40   True
1    b       2    4            40   True
2    c       3    9            40   True
3    d       4   16            40   True
4    e       5   25            40   True
5    f       6   36            40   True
6    g       7   49            40  False
7    h       8   64            40  False
8    i       9   81            40  False

答案 1 :(得分:2)

这是我的创意方法:

数据:

In [23]: df
Out[23]:
  Col1  Number
0    a       1
1    b       2
2    c       3
3    d       4
4    e       5
5    f       6
6    g       7
7    h       8
8    i       9

<强>解决方案:

In [24]: df['Type'] = 'number' + (1 + ((df.Number**2)>40).astype(int)).astype(str)

<强>结果:

In [25]: df
Out[25]:
  Col1  Number     Type
0    a       1  number1
1    b       2  number1
2    c       3  number1
3    d       4  number1
4    e       5  number1
5    f       6  number1
6    g       7  number2
7    h       8  number2
8    i       9  number2

<强>解释

In [29]: ((df.Number**2)>40).astype(int)
Out[29]:
0    0
1    0
2    0
3    0
4    0
5    0
6    1
7    1
8    1
Name: Number, dtype: int32

In [30]: 1 + ((df.Number**2)>40).astype(int)
Out[30]:
0    1
1    1
2    1
3    1
4    1
5    1
6    2
7    2
8    2
Name: Number, dtype: int32

答案 2 :(得分:1)

制作自定义功能,然后在pandas.apply

中使用它
import pandas as pd

# Rule to produce new values
def f(row):
    if row['Number']**2 > 40:
        val = 'Number2'
    else:
        val = 'Number1'
    return val

df = pd.DataFrame({'Number': [1,2,3,4,5,6,7,8,9], 'Col1':['a','b','c','d','e','f','g','h','i']})

# Apply the function to construct new column
df['Type'] = df.apply(f, axis=1)
print (df)

输出:

 Col1  Number     Type
0    a       1  Number1
1    b       2  Number1
2    c       3  Number1
3    d       4  Number1
4    e       5  Number1
5    f       6  Number1
6    g       7  Number2
7    h       8  Number2
8    i       9  Number2