我有一张股票交易表,看起来像这样...
该帐号可能重复多次,该帐号也可能多次订购同一产品。
+------------+------------+------------+--------+---------+--------------+
| SA_ACCOUNT | SA_TRDATE | SA_TRVALUE | SA_QTY | SA_COST | SA_PRODUCT |
+------------+------------+------------+--------+---------+--------------+
| CSU1 | 23/03/2017 | 21.01 | 1 | 30 | W100/18 |
| AAA1 | 12/07/2018 | 38.04 | 6 | 19.8 | GPLR03REC800 |
| BWR1 | 01/11/2018 | 0 | -1 | 0 | W562/20 |
| CNT1 | 01/11/2018 | -2.22 | -1 | -1.23 | RX613S/12 |
| GBH1 | 15/09/2017 | 0 | 1 | 0 | COR2 |
+------------+------------+------------+--------+---------+--------------+
我想输出一个表格,其中每个帐户都为一行,所有产品为一列-该客户的总销售价值和该客户的总pcs。
预期的输出(比下面的示例多得多的列):
+---------+----------+------------+---------------+-----------------+--------------+----------------+-----------+
| Account | MISC_PCS | MISC_VALUE | RX613S/12_PCS | RX613S/12_VALUE | R623S/12_PCS | R623S/12_VALUE | SP377_PCS |
+---------+----------+------------+---------------+-----------------+--------------+----------------+-----------+
| AGT1 | 25 | 32.65 | 2 | 5.26 | 0 | 0 | 0 |
| AHB1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| AHB2 | 0 | 0 | 0 | 0 | 2 | 1.25 | 0 |
| AJB1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| AJE2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| AJT4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| AJW1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| AK11 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| AKS1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---------+----------+------------+---------------+-----------------+--------------+----------------+-----------+
我编写了以下代码,但是它的运行速度非常慢,尽管它无法使用(我要遍历30万行)
谁能提供更好的解决方案?
我的代码:
acc=""
index_test = -1
test_df = pd.DataFrame()
#For every row in the dataframe, iterate through the list of genres and place a 1 into the corresponding column
for index, row in stock_tran_df.iterrows():
if acc != row["SA_DACCNT"]:
acc = row["SA_DACCNT"]
print(acc)
index_test += 1
test_df.loc[index_test,"Account"] = acc
try:
test_df.loc[index_test,row["SA_PRODUCT"] + "_PCS"] = test_df.loc[index_test,row["SA_PRODUCT"] + "_PCS"] + row["SA_QTY"]
test_df.loc[index_test,row["SA_PRODUCT"] + "_VALUE"] = test_df.loc[index_test,row["SA_PRODUCT"] + "_VALUE"] + row["SA_TRVALUE"]
except:
test_df.loc[index_test,row["SA_PRODUCT"] + "_PCS"] = row["SA_QTY"]
test_df.loc[index_test,row["SA_PRODUCT"] + "_VALUE"] = row["SA_TRVALUE"]
test_df.fillna(0,inplace=True)
答案 0 :(得分:1)
看起来您正在寻找的是
具有参数table=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
number = input()
# loop over the input string to get number by number
for x in number:
# now get the string representation from its index in table
print(table[int(x)], end=' ')
的功能