パンダは初めてなので、明らかな解決策があるとすみません... 2列しかないCSVをインポートし、3列目を作成しました。上位10行とヘッダーのスクリーンショットは次のとおりです。DataFrameのスクリーンショット
['Amount Changed']列で最小値と最大値を見つける方法を理解しましたが、最小値と最大値に関連付けられた日付を取得する必要がありますが、インデックスと['Profit / Loss']は取得しません。iloc、loc、groupbyについて読んでみました-再び使用できる単一の値(この場合は日付)を返すようにそれらのいずれも取得できません。
私の目標は、['Amount Changed']の最大値と同じ行にあるが、['Date']列の日付に関連付けられている新しい変数 'Gi_Date'を作成することです。
印刷ステートメントで使用したり、txtファイルに書き込んだりできるように、変数を分離しておくようにしています。
import os
import csv
import pandas as pd
import numpy as np
#path for CSV file
csvpath = ("budget_data.csv")
#Read CSV into Panadas and give it a variable name Bank_pd
Bank_pd = pd.read_csv(csvpath, parse_dates=True)
#Number of month records in the CSV
Months = Bank_pd["Date"].count()
#Total amount of money captured in the data converted to currency
Total_Funds = '${:.0f}'.format(Bank_pd["Profit/Losses"].sum())
#Determine the amount of increase or decrease from the previous month
AmtChange = Bank_pd["Profit/Losses"].diff()
Bank_pd["Amount Changed"] = AmtChange
#Identify the greatest positive change
GreatestIncrease = '${:.0f}'.format(Bank_pd["Amount Changed"].max())
Gi_Date = Bank_pd[Bank_pd["Date"] == GreatestIncrease]
#Identify the greatest negative change
GreatestDecrease = '${:.0f}'.format(Bank_pd["Amount Changed"].min())
Gd_Date = Bank_pd[Bank_pd['Date'] == GreatestDecrease]
print(f"Total Months: {Months}")
print(f"Total: {Total_Funds}")
print(f"Greatest Increase in Profits: {Gi_Date} ({GreatestIncrease})")
print(f"Greatest Decrease in Profits: {Gd_Date} ({GreatestDecrease})")
スクリプトをgitbashで実行すると、エラーが発生しなくなったので、次のように表示される日付を表示するのではなく、近づいていると思います。
$ python PyBank.py
Total Months: 86
Total: $38382578
Greatest Increase in Profits: Empty DataFrame
Columns: [Date, Profit/Losses, Amount Changed]
Index: [] ($1926159)
Greatest Decrease in Profits: Empty DataFrame
Columns: [Date, Profit/Losses, Amount Changed]
Index: [] ($-2196167)
私はそれをこのように印刷したい:
$ python PyBank.py
Total Months: 86
Total: $38382578
Greatest Increase in Profits: Feb-2012 ($1926159)
Greatest Decrease in Profits: Sept-2013 ($-2196167)
元のDataFrameの1年分の価値は次のとおりです。
bank_pd = pd.DataFrame({'Date':['Jan-10', 'Feb-10', 'Mar-10', 'Apl-10', 'May-10', 'Jun-10', 'Jul-10', 'Aug-10', 'Sep-10', 'Oct-10', 'Nov-10', 'Dec-10'],
'Profit/Losses':[867884, 984655, 322013, -69417, 310503, 522857, 1033096, 604885, -216386, 477532, 893810, -80353]})
サンプルdfで期待される出力は次のようになります。合計月数:12合計資金:$ 5651079利益の最大の増加:10月10日($ 693918)利益の最大の減少:12月10日($ -974163)
上からのサンプルデータフレームにもエラーがありました。すばやく入力したときに1か月間欠落していましたが、現在は修正されています。
ありがとう!
使用されている変数にいくつかの不具合が見られます。
Bank_pd["Amount Changed"] = AmtChange
上記のステートメントは、実際にはデータフレームを「AmountChanged」列に置き換えています。このステートメントの後、この列を任意の操作に使用できます。
以下は更新されたコードであり、新しく追加された行を強調しています。さらにフォーマットを追加できます。
import pandas as pd
csvpath = ("budget_data.csv")
Bank_pd = pd.read_csv(csvpath, parse_dates=True)
inp_bank_pd = pd.DataFrame(Bank_pd)
Months = Bank_pd["Date"].count()
Total_Funds = '${:.0f}'.format(Bank_pd["Profit/Losses"].sum())
AmtChange = Bank_pd["Profit/Losses"].diff()
GreatestIncrease = Bank_pd["Amount Changed"].max()
Gi_Date = inp_bank_pd.loc[Bank_pd["Amount Changed"] == GreatestIncrease]
print(Months)
print(Total_Funds)
print(Gi_Date['Date'].values[0])
print(GreatestIncrease)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加