2016-04-24 73 views
0
import re 
#Creating several new colums with a for loop and adding them to the original df. 
#Creating permutations for a second level of binary variables for df 
for i in list_ib: 
    for j in list_ib: 
     if i == j: 
      break 
     else:    
      bina = df[i]*df[j] 
      print(i,j) 

i是屬於數據幀(df)的二進制列,j是相同的列。 我已經計算了每列與每列的乘法。我現在的問題是,如何將所有新的二進制產品列添加到原始df中?將列添加到由python中for循環計算的數據幀中

我曾嘗試:

df = df + df[i,j,bina] 

,但我沒有得到我需要的結果。有什麼建議麼?

回答

0

據我所知,i,j,bina不是你的df的一部分。構建陣列的那些中的每一個,代表一個「行」和一旦有了所有行爲i,j,bina準備好了,然後就可以串聯這樣每個陣列元素:一旦已經收集

>>> new_df = pd.DataFrame(data={'i':i, 'j':j, 'bina':bina}, columns=['i','j','bina']) 
>>> pd.concat([df, new_df], axis=1) 

或者,對於'i', 'j' and 'bina'所有數據假設你有一個單獨的數組對於這些數據,你可以這樣做:

>>> df['i'] = i 
>>> df['j'] = j 
>>> df['bina'] = bina 

如果這三個數組有一樣多的元素在數據幀DF行。這隻會工作。

我希望這有助於!

+0

找到了你之後? – Thanos

0

通常,您可以使用其內置的__setitem__()將列添加到Dataframe,您可以通過[]訪問該列。例如:

import pandas as pd 

df = pd.DataFrame() 

df["one"] = 1, 1, 1 
df["two"] = 2, 2, 2 
df["three"] = 3, 3, 3 

print df 

# Output: 
# one two three 
# 0 1 2  3 
# 1 1 2  3 
# 2 1 2  3 

list_ib = df.columns.values 

for i in list_ib: 
    for j in list_ib: 
     if i == j: 
      break 
     else: 
      bina = df[i] * df[j] 
      df['bina_' + str(i) + '_' + str(j)] = bina # Add new column which is the result of multiplying columns i and j together 

print df 

# Output: 
#  one two three bina_two_one bina_three_one bina_three_two 
# 0 1 2  3    2    3    6 
# 1 1 2  3    2    3    6 
# 2 1 2  3    2    3    6 
相關問題