Как я могу сохранить пользовательские данные в пандах, не перезаписывая данные в одной строке - PullRequest
0 голосов
/ 30 июня 2018

Всякий раз, когда я пытаюсь принять новый пользовательский ввод, он перезаписывает старый

import pandas as pd

pd.set_option('display.max_columns', 50)


Comp_Name=input("Company Name/Symbol")

NI_before_Extraordinary_items=int(input("Net income before extraordinary items")) 

TA_beg_yr=int(input('Total assets at the beginning of the year'))

Cash_from_op=int(input("Cash flow from operations"))

Prev_NI_before_Extraordinary_items=int(input("Net income before extraordinary items of the last year"))

TA_t2=int(input('Beginning total assets of the previous year'))

for i in range(0,30):
    Company_stats={'index':[i], 'Company Name':[Comp_Name], 'Net Income': 
    [NI_before_Extraordinary_items],'Total Assets At Beg.of the year': 
    [TA_beg_yr],'Cash flow from operations':Cash_from_op}
    df=pd.DataFrame(Company_stats)
    df.set_index('index',inplace=True)
    i=i+1
    print(df)

Я хочу знать, могу ли я сохранить свои входные данные подряд, а затем, когда программа снова запустится, она сохранит входные данные в следующей строке и т. Д.

1 Ответ

0 голосов
/ 01 июля 2018

Попробуйте:

import pandas as pd
pd.set_option('display.max_columns', 50)
Comp_Name=input("Company Name/Symbol")
NI_before_Extraordinary_items=int(input("Net income before extraordinary items")) 
TA_beg_yr=int(input('Total assets at the beginning of the year'))
Cash_from_op=int(input("Cash flow from operations"))
Prev_NI_before_Extraordinary_items=int(input("Net income before extraordinary items of the last year"))
TA_t2=int(input('Beginning total assets of the previous year'))

for i in range(0,30):
    Company_stats={'index':[i], 'Company Name':[Comp_Name], 'Net Income': 
    [NI_before_Extraordinary_items],'Total Assets At Beg.of the year': 
    [TA_beg_yr],'Cash flow from operations':Cash_from_op}
    df=pd.DataFrame(Company_stats)
    #df.set_index('index',inplace=True)  <-- this is your problem...
    i=i+1
    #print(df)

ввод:

aaa, 111, 222, 333, 444, 555
Вывод

.to_dict(), чтобы его было легче читать):

In [356]: df.to_dict()
Out[356]: 
{'Cash flow from operations': {0: 333},
 'Company Name': {0: 'aaa'},
 'Net Income': {0: 111},
 'Total Assets At Beg.of the year': {0: 222},
 'index': {0: 29}}  

.set_index() метод передает column label или list of column labels. Поскольку он находится в цикле for, он сбрасывается при каждой итерации. Вам нужно будет переместить его за пределы цикла, чтобы он был эффективным.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...