I have a code which throwing warning message
FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
data = data.append(row, ignore_index=True)
Question 1 : I want to know how to use concat instead of append in below code.
import pandas as pd
from datetime import datetime
data = pd.DataFrame()
for i in range(30):
value = datetime.now()
value1 = i
value2 = i 20
value3 = i - 10
if value3 >= 15:
row = pd.Series([value, value1, value2, value3])
data = data.append(row, ignore_index=True)
data.columns = ['Time', 'Entry Price', 'Target', 'Stop Loss']
print(data)
Question 2: "If" condition value will be changing in my actual code. And sometime its expected that result will be null data frame.
eg :
if value3 >= 20:
row = pd.Series([value, value1, value2, value3])
data = data.append(row, ignore_index=True)
data.columns = ['Time', 'Entry Price', 'Target', 'Stop Loss']
print(data)
This is throwing error
File "C:\Users\jayan\.conda\envs\TestEnvironment\lib\site-packages\pandas\core\internals\base.py", line 70, in _validate_set_axis
raise ValueError(
ValueError: Length mismatch: Expected axis has 0 elements, new values have 4 elements
I tried the code as below, but the output is not in expected format.
import pandas as pd
from datetime import datetime
data = pd.DataFrame()
for i in range(30):
value = datetime.now()
value1 = i
value2 = i 20
value3 = i - 10
if value3 >= 15:
row = pd.DataFrame([value, value1, value2, value3])
data = pd.concat([data, row])
print(data)
OUTPUT
runfile('C:/AlgoTrade/test/untitled0.py', wdir='C:/AlgoTrade/test')
0
0 2022-11-11 17:06:29.709333
1 25
2 45
3 15
0 2022-11-11 17:06:29.709333
1 26
2 46
3 16
0 2022-11-11 17:06:29.709333
1 27
2 47
3 17
0 2022-11-11 17:06:29.710333
1 28
2 48
3 18
0 2022-11-11 17:06:29.710333
1 29
2 49
3 19
Expected output should be in below format without any warning message.
runfile('C:/AlgoTrade/test/untitled0.py', wdir='C:/AlgoTrade/test')
Time Entry Price Target Stop Loss
0 2022-11-11 17:09:32.104801 25 45 15
1 2022-11-11 17:09:32.104801 26 46 16
2 2022-11-11 17:09:32.104801 27 47 17
3 2022-11-11 17:09:32.104801 28 48 18
4 2022-11-11 17:09:32.104801 29 49 19
CodePudding user response:
Maintain sep list and append them.
import pandas as pd
from datetime import datetime
l1=[]
l2=[]
l3=[]
l4=[]
data = pd.DataFrame()
for i in range(30):
value = datetime.now()
value1 = i
value2 = i 20
value3 = i - 10
if value3 >= 15:
l1.append(value)
l2.append(value1)
l3.append(value2)
l4.append(value3)
data = pd.DataFrame(
{'Time': l1,
'Entry Price': l2,
'Target': l3,
'Stop Loss':l4
})
print(data)
output #
Time Entry Price Target Stop Loss
0 2022-11-11 17:47:32.846271 25 45 15
1 2022-11-11 17:47:32.846271 26 46 16
2 2022-11-11 17:47:32.846271 27 47 17
3 2022-11-11 17:47:32.846271 28 48 18
4 2022-11-11 17:47:32.846271 29 49 19