I need to create a dataframe with two columns: variable, function based on this variable. There is an error in case of next code:
JavaScript
x
9
1
test = pd.DataFrame({'Column_1': pd.Series([], dtype='int'),
2
'Column_2': pd.Series([], dtype='float')})
3
for i in range(1,30):
4
k = 0.5**i
5
test.append(i, k)
6
print(test)
7
8
TypeError: cannot concatenate object of type '<class 'int'>'; only Series and DataFrame objs are valid
9
What do I need to fix here? Looks like answer is easy, however it is uneasy to find it… Many thanks for your help
Advertisement
Answer
I like Vaishali’s way of approaching it. If you really want to use the for loop, this is how I would of done it:
JavaScript
1
20
20
1
import pandas as pd
2
3
test = pd.DataFrame({'Column_1': pd.Series([], dtype='int'),
4
'Column_2': pd.Series([], dtype='float')})
5
6
for i in range(1,30):
7
test=test.append({'Column_1':i,'Column_2':0.5**i},ignore_index=True)
8
9
test = test.round(5)
10
print(test)
11
12
13
Column_1 Column_2
14
0 1.0 0.50000
15
1 2.0 0.25000
16
2 3.0 0.12500
17
3 4.0 0.06250
18
4 5.0 0.03125
19
5 6.0 0.01562
20