Python / Pandas:基于单元格值的计算

时间:2017-08-04 06:54:02

标签: python pandas dataframe

我有这样的数据框:

let account = ACAccountStore()
let accountType = account.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
let options: [AnyHashable : Any] = [ACFacebookAppIdKey: "Your App ID on FB", ACFacebookPermissionsKey: ["publish_stream", "publish_actions"], ACFacebookAudienceKey: ACFacebookAudienceEveryone]

account.requestAccessToAccounts(with: accountType, options: options) { (success, error)  in
  if success {
    if let accounts = account.accounts(with: accountType) {
      if accounts.isEmpty {
        print("No facebook account found, please add your facebook account in phone settings")
      } else {
        let facebookAccount = accounts.first as! ACAccount

        let message = ["status": "My first Facebook posting "]
        let requestURL = URL(string: "https://graph.facebook.com/me/feed")

        let postRequest = SLRequest(forServiceType: SLServiceTypeFacebook,
                                    requestMethod: SLRequestMethod.POST,
                                    url: requestURL,
                                    parameters: message)
        postRequest?.account = facebookAccount

        postRequest?.perform(handler: {(_, urlResponse,
          error) in

          if let err = error {
            print("Error : \(err.localizedDescription)")
          }
          print("Facebook HTTP response \(String(describing: urlResponse?.statusCode))")
        })
      }
    }
  } else {
    print("Facebook account error: \(String(describing: error))")
  }
}

我需要做这样的事情:

   A B C D E
0  2 3 4 8 7
1  4 7 5 9 4 
2  3 4 5 7 2
3  8 9 1 3 7

我尝试以几种方式做到这一点,例如:

if 'value in column A' == 2:
  'value for this row in new column' = 'value from column B' + 'value from column C'
elif 'value in column A' == 4:
   'value for this row in new column' = 'value from column B' + 'value from column D'
elif 'value in column A' == 8:
   'value for this row in new column' = 'value from column B' + 'value from column E'
else:
   'value for this row in new column' = 0

但结果中我有空单元格。

3 个答案:

答案 0 :(得分:5)

另一种简单的方法是使用字典和lookup来获得总和,即

colons = {2: 'C', 4: 'D', 8: 'E'}
df['sum']= np.nan
df['sum'] = df['B']+ df.lookup(df['A'].index,df['A'].map(colons).fillna('sum'))

输出:

   A  B  C  D  E   sum
0  2  3  4  8  7   7.0
1  4  7  5  9  4  16.0
2  3  4  5  7  2   NaN
3  8  9  1  3  7  16.0

您可以使用df.fillna(0)

将0填充为0

答案 1 :(得分:1)

这是一种方法

def f1(x):
    if x['A']==2:
        return x['B'] + x['C']
    elif x['A']==4:
        return x['B'] + x['D']
    elif x['A']==8:
        return x['B'] + x['E']
    else:
        return 0

df['sum'] = df.apply(f1 , axis=1)
df.head()

输出:

A   B   C   D   E   sum
2   3   4   8   7   7
4   7   5   9   4   16
3   4   5   7   2   0
8   9   1   3   7   16

答案 2 :(得分:0)

您正在获得NAs,因为未涵盖df.A == 3个案例。使用df.loc[:,'sum'] = 0 # or any other starting value来避免此

A =[2, 4, 3, 8]
B =[3, 7, 4, 9]
C =[4, 5, 5, 1]
D =[8, 9, 7, 3]
E =[7, 4, 2, 7]

_all = [A,B,C,D,E]
df = pd.DataFrame(_all, columns = ['A', 'B', 'C', 'D', 'E'])

df.loc[:,'sum'] = 0
df.loc[:,'sum'][df['A'] == 2] = df['B'] + df['C']
df.loc[:,'sum'][df['A'] == 4] = df['B'] + df['D']
df.loc[:,'sum'][df['A'] == 8] = df['B'] + df['E']

>>> df
   A  B  C  D  E  sum
0  2  3  4  8  7    7
1  4  7  5  9  4   16
2  3  4  5  7  2    0
3  8  9  1  3  7   16