我尝试了几种不同的方法来解决这个问题,但没有一种能帮助我解决问题。
我正在从Finanzblick导出csv数据,我有一个名为amount
的数组。如果此数组中的值'(例如19.34
)为正(大于零),则应将其传输到数组inflow
。如果该值为负(例如-19.34
),则应将其转移到“outflow”
import pandas as pd
import numpy as np
from pandas.core.tools.numeric import to_numeric
df=pd.read_csv("C:/Users/PD/Desktop/Finanzblick Dokumente/2017_11/2017_11-
DB.csv", sep=';',usecols=(0,1,2,3,4), encoding='utf-8', decimal=',')
df.columns = ['Date', 'Payee', 'Verwendungszweck', 'Buchungstext', 'Betrag']
df['Memo'] = df[['Buchungstext', 'Verwendungszweck']].apply(lambda x: ' -- '.join(x), axis=1)
Betrag = df.Betrag.astype(int)
df['Inflow'] = np.where(df.Betrag > 0, df.Betrag, "")
df['Outflow'] = np.where(df.Betrag < 0, df.Betrag*(-1), "")
df.to_csv('C:/Users/PD/source/repos/Finanzblick YNAB/Finanzblick YNAB/2017_11-DB-import.csv',sep=';', index = False, columns=['Date', 'Payee', 'Memo', 'Inflow', 'Outflow'], decimal='.')
问候菲尔
答案 0 :(得分:0)
如果您的正数和负数都在数组中,则以下内容将用符号分隔:
inflows = np.where(amount > 0, amount, 0.)
outflows = np.where(amount < 0, amount, 0.)
如果它们位于DataFrame中,请使用:
df['inflows'] = np.where(df.amount > 0, df.amount, 0.)
df['outflows'] = np.where(df.amount < 0, df.amount, 0.)