Reputation: 3528
I am trying to create a df
based on the user inputs as follows:
import pandas as pd
data = pd.DataFrame(columns=['ID','QTY', 'MOA', 'TAX'])
while True:
add_seg = input('Do you want to add a segment (Y/N)?: ')
if (add_seg == 'Y')|(add_seg == 'y'):
id_value = input('ID ')
qty_value = input('QTY ')
moa_value = input('MOA ')
tax_value = input('TAX ')
data['ID'].append(int(id_value))
data['QTY'].append(int(qty_value))
data['MOA'].append(int(moa_value))
data['TAX'].append(int(tax_value))
else:
break
Whenever the user wants to enter a segment, he selects Y
and starts entering the values. Now, I want to add these values to a df
too. I tried appending it, but it gives me the following error:
Traceback (most recent call last):
File "", line 13, in data['ID'].append(int(id_value))
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\series.py", line 2775, in append to_concat, ignore_index=ignore_index, verify_integrity=verify_integrity
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\reshape\concat.py", line 255, in concat sort=sort,
File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\pandas\core\reshape\concat.py", line 332, in init raise TypeError(msg)
TypeError: cannot concatenate object of type ''; only Series and DataFrame objs are valid
Example:
Do you want to add a segment (Y/N)?: y
ID 66
QTY 654
MOA 6565
TAX 6
Do you want to add a segment (Y/N)?: y
ID 656
QTY 61
MOA 65
TAX 64651
and from these values, a df
must be formed as follows:
ID QTY MOA TAX
66 654 6565 6
656 61 65 64651
How can this be done?
Upvotes: 2
Views: 1110
Reputation: 1909
Alternatively, append empty row and then set its columns:
while True:
add_seg = input('Do you want to add a segment (Y/N)?: ')
if (add_seg == 'Y')|(add_seg == 'y'):
data = data.append({}, ignore_index=True)
last_row = data.iloc[-1];
for c in data.columns:
last_row[c] = int(input(c))
else:
break
Upvotes: 0
Reputation: 3495
After the inputs use:
data.loc[len(data)] = [int(id_value), int(qty_value), int(moa_value), int(tax_value)]
Upvotes: 1
Reputation: 862681
You can crete dictionary and use DataFrame.append
:
import pandas as pd
data = pd.DataFrame(columns=['ID','QTY', 'MOA', 'TAX'])
while True:
add_seg = input('Do you want to add a segment (Y/N)?: ')
if (add_seg == 'Y')|(add_seg == 'y'):
id_value = input('ID ')
qty_value = input('QTY ')
moa_value = input('MOA ')
tax_value = input('TAX ')
d = {'ID':int(id_value),'QTY':int(qty_value),'MOA':int(moa_value),'TAX':int(tax_value)}
data = data.append(d, ignore_index=True)
else:
break
print (data)
Another idea is create list of dictionaries with python append
and last call DataFrame
constructor:
L = []
while True:
add_seg = input('Do you want to add a segment (Y/N)?: ')
if (add_seg == 'Y')|(add_seg == 'y'):
id_value = input('ID ')
qty_value = input('QTY ')
moa_value = input('MOA ')
tax_value = input('TAX ')
d = {'ID':int(id_value),'QTY':int(qty_value),'MOA':int(moa_value),'TAX':int(tax_value)}
L.append(d)
else:
break
data = pd.DataFrame(L, columns=['ID','QTY', 'MOA', 'TAX'])
print (data)
Upvotes: 3