Data frame misaligned using Pandas on Python3
up vote
1
down vote
favorite
I have a data
which I am trying to store in pandas
dataFrame. But, it is appearing in a weird way. I know I am doing something wrong
Can somebody help me in finding whats wrong.
Code
root@optstra:~# cat pandas_1.py
import pandas as pd
import numpy as np
numberOfRows = 1
SYMBOL = 'ABB'
volume_increasing = True
price_increase = True
OI_CHANGE = True
closedAboveYesterday = False
Above_22SMA = False
data_frame = pd.DataFrame(index=np.arange(0, numberOfRows), columns=('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA') )
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA for n in range(6)]
print(data_frame)
Output
root@optstra:~# python3 pandas_1.py
SYMBOL Volume Price OI OHLC 22SMA
0 False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB
If I change the line which writes the data to data frame as follows
for x in range(0,numberOfRows):
data_frame.loc[x] = [(SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA) for n in range(6)]
Output changes to
root@optstra:~# python3 pandas_1.py
SYMBOL ... 22SMA
0 (ABB, True, True, True, False, False) ... (ABB, True, True, True, False, False)
python pandas
|
show 2 more comments
up vote
1
down vote
favorite
I have a data
which I am trying to store in pandas
dataFrame. But, it is appearing in a weird way. I know I am doing something wrong
Can somebody help me in finding whats wrong.
Code
root@optstra:~# cat pandas_1.py
import pandas as pd
import numpy as np
numberOfRows = 1
SYMBOL = 'ABB'
volume_increasing = True
price_increase = True
OI_CHANGE = True
closedAboveYesterday = False
Above_22SMA = False
data_frame = pd.DataFrame(index=np.arange(0, numberOfRows), columns=('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA') )
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA for n in range(6)]
print(data_frame)
Output
root@optstra:~# python3 pandas_1.py
SYMBOL Volume Price OI OHLC 22SMA
0 False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB
If I change the line which writes the data to data frame as follows
for x in range(0,numberOfRows):
data_frame.loc[x] = [(SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA) for n in range(6)]
Output changes to
root@optstra:~# python3 pandas_1.py
SYMBOL ... 22SMA
0 (ABB, True, True, True, False, False) ... (ABB, True, True, True, False, False)
python pandas
What format isdata
? Can you show us in your question?
– jpp
Nov 11 at 11:16
@jpp they areboolean
andstring
if you see I have data which in the form ofvariables
which I want to organise in tabular format and convert to csv. I am trying to map them like SYMBOL "variable"
– Vinay Shukla
Nov 11 at 11:18
No, I don't see... all I see isSyntaxError
because you haven't defined any of your variables,SYMBOL
etc in your question.
– jpp
Nov 11 at 11:19
Its a running code though I will edit it or create a pseudo code
– Vinay Shukla
Nov 11 at 11:20
1
Where is the data for PCJEWELLER coming from?
– leeym
Nov 11 at 11:25
|
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a data
which I am trying to store in pandas
dataFrame. But, it is appearing in a weird way. I know I am doing something wrong
Can somebody help me in finding whats wrong.
Code
root@optstra:~# cat pandas_1.py
import pandas as pd
import numpy as np
numberOfRows = 1
SYMBOL = 'ABB'
volume_increasing = True
price_increase = True
OI_CHANGE = True
closedAboveYesterday = False
Above_22SMA = False
data_frame = pd.DataFrame(index=np.arange(0, numberOfRows), columns=('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA') )
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA for n in range(6)]
print(data_frame)
Output
root@optstra:~# python3 pandas_1.py
SYMBOL Volume Price OI OHLC 22SMA
0 False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB
If I change the line which writes the data to data frame as follows
for x in range(0,numberOfRows):
data_frame.loc[x] = [(SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA) for n in range(6)]
Output changes to
root@optstra:~# python3 pandas_1.py
SYMBOL ... 22SMA
0 (ABB, True, True, True, False, False) ... (ABB, True, True, True, False, False)
python pandas
I have a data
which I am trying to store in pandas
dataFrame. But, it is appearing in a weird way. I know I am doing something wrong
Can somebody help me in finding whats wrong.
Code
root@optstra:~# cat pandas_1.py
import pandas as pd
import numpy as np
numberOfRows = 1
SYMBOL = 'ABB'
volume_increasing = True
price_increase = True
OI_CHANGE = True
closedAboveYesterday = False
Above_22SMA = False
data_frame = pd.DataFrame(index=np.arange(0, numberOfRows), columns=('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA') )
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA for n in range(6)]
print(data_frame)
Output
root@optstra:~# python3 pandas_1.py
SYMBOL Volume Price OI OHLC 22SMA
0 False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB False, True, ABB
If I change the line which writes the data to data frame as follows
for x in range(0,numberOfRows):
data_frame.loc[x] = [(SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA) for n in range(6)]
Output changes to
root@optstra:~# python3 pandas_1.py
SYMBOL ... 22SMA
0 (ABB, True, True, True, False, False) ... (ABB, True, True, True, False, False)
python pandas
python pandas
edited Nov 11 at 11:28
asked Nov 11 at 11:09
Vinay Shukla
1,345627
1,345627
What format isdata
? Can you show us in your question?
– jpp
Nov 11 at 11:16
@jpp they areboolean
andstring
if you see I have data which in the form ofvariables
which I want to organise in tabular format and convert to csv. I am trying to map them like SYMBOL "variable"
– Vinay Shukla
Nov 11 at 11:18
No, I don't see... all I see isSyntaxError
because you haven't defined any of your variables,SYMBOL
etc in your question.
– jpp
Nov 11 at 11:19
Its a running code though I will edit it or create a pseudo code
– Vinay Shukla
Nov 11 at 11:20
1
Where is the data for PCJEWELLER coming from?
– leeym
Nov 11 at 11:25
|
show 2 more comments
What format isdata
? Can you show us in your question?
– jpp
Nov 11 at 11:16
@jpp they areboolean
andstring
if you see I have data which in the form ofvariables
which I want to organise in tabular format and convert to csv. I am trying to map them like SYMBOL "variable"
– Vinay Shukla
Nov 11 at 11:18
No, I don't see... all I see isSyntaxError
because you haven't defined any of your variables,SYMBOL
etc in your question.
– jpp
Nov 11 at 11:19
Its a running code though I will edit it or create a pseudo code
– Vinay Shukla
Nov 11 at 11:20
1
Where is the data for PCJEWELLER coming from?
– leeym
Nov 11 at 11:25
What format is
data
? Can you show us in your question?– jpp
Nov 11 at 11:16
What format is
data
? Can you show us in your question?– jpp
Nov 11 at 11:16
@jpp they are
boolean
and string
if you see I have data which in the form of variables
which I want to organise in tabular format and convert to csv. I am trying to map them like SYMBOL "variable"– Vinay Shukla
Nov 11 at 11:18
@jpp they are
boolean
and string
if you see I have data which in the form of variables
which I want to organise in tabular format and convert to csv. I am trying to map them like SYMBOL "variable"– Vinay Shukla
Nov 11 at 11:18
No, I don't see... all I see is
SyntaxError
because you haven't defined any of your variables, SYMBOL
etc in your question.– jpp
Nov 11 at 11:19
No, I don't see... all I see is
SyntaxError
because you haven't defined any of your variables, SYMBOL
etc in your question.– jpp
Nov 11 at 11:19
Its a running code though I will edit it or create a pseudo code
– Vinay Shukla
Nov 11 at 11:20
Its a running code though I will edit it or create a pseudo code
– Vinay Shukla
Nov 11 at 11:20
1
1
Where is the data for PCJEWELLER coming from?
– leeym
Nov 11 at 11:25
Where is the data for PCJEWELLER coming from?
– leeym
Nov 11 at 11:25
|
show 2 more comments
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Updating an empty frame (e.g. using loc one-row-at-a-time) is inefficient.
So better/faster is create list by append with DataFrame
contructor:
data =
for x in np.arange(numberOfRows):
row = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
data.append(row)
c = ('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA')
data_frame = pd.DataFrame(data, columns=c)
list comprehension alternative
:
data = [[SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA] for x in np.arange(numberOfRows)]
1
This should be the accepted answer.pd.DataFrame.loc
in a loop is inefficient.
– jpp
Nov 11 at 11:56
add a comment |
up vote
2
down vote
Why don't you try this-- not sure if it's exactly what you're looking for since you took that part out in your edit:
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
Output:
SYMBOL Volume Price OI OHLC 22SMA
0 ABB True True True False False
1
Thank you !! It was a silly question as I am new to this language :)
– Vinay Shukla
Nov 11 at 11:50
add a comment |
up vote
0
down vote
It seems to me you're not quite indexing the dataframe properly. You can either do this:
for x in range(0, numberOfRows):
data_frame['SYMBOL'][x] = SYMBOL
data_frame['Volume'][x] = volume_increasing
data_frame['Price'][x] = price_increase
data_frame['OI'][x] = OI_CHANGE
data_frame['OHLC'][x] = closedAboveYesterday
data_frame['22SMA'][x] = Above_22SMA
which will give you your desired output, alternatively you can use dictionaries and avoid the for loop altogether:
columns = ['SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA']
data = 'SYMBOL': 'AAB',
'Volume': True,
'Price': True,
'OI': True,
'OHLC': False,
'22SMA': False
data_frame = pd.DataFrame(data=data, index=np.arange(0, 1), columns=columns)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Updating an empty frame (e.g. using loc one-row-at-a-time) is inefficient.
So better/faster is create list by append with DataFrame
contructor:
data =
for x in np.arange(numberOfRows):
row = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
data.append(row)
c = ('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA')
data_frame = pd.DataFrame(data, columns=c)
list comprehension alternative
:
data = [[SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA] for x in np.arange(numberOfRows)]
1
This should be the accepted answer.pd.DataFrame.loc
in a loop is inefficient.
– jpp
Nov 11 at 11:56
add a comment |
up vote
2
down vote
accepted
Updating an empty frame (e.g. using loc one-row-at-a-time) is inefficient.
So better/faster is create list by append with DataFrame
contructor:
data =
for x in np.arange(numberOfRows):
row = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
data.append(row)
c = ('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA')
data_frame = pd.DataFrame(data, columns=c)
list comprehension alternative
:
data = [[SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA] for x in np.arange(numberOfRows)]
1
This should be the accepted answer.pd.DataFrame.loc
in a loop is inefficient.
– jpp
Nov 11 at 11:56
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Updating an empty frame (e.g. using loc one-row-at-a-time) is inefficient.
So better/faster is create list by append with DataFrame
contructor:
data =
for x in np.arange(numberOfRows):
row = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
data.append(row)
c = ('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA')
data_frame = pd.DataFrame(data, columns=c)
list comprehension alternative
:
data = [[SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA] for x in np.arange(numberOfRows)]
Updating an empty frame (e.g. using loc one-row-at-a-time) is inefficient.
So better/faster is create list by append with DataFrame
contructor:
data =
for x in np.arange(numberOfRows):
row = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
data.append(row)
c = ('SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA')
data_frame = pd.DataFrame(data, columns=c)
list comprehension alternative
:
data = [[SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA] for x in np.arange(numberOfRows)]
edited Nov 11 at 12:02
answered Nov 11 at 11:55
jezrael
312k21247323
312k21247323
1
This should be the accepted answer.pd.DataFrame.loc
in a loop is inefficient.
– jpp
Nov 11 at 11:56
add a comment |
1
This should be the accepted answer.pd.DataFrame.loc
in a loop is inefficient.
– jpp
Nov 11 at 11:56
1
1
This should be the accepted answer.
pd.DataFrame.loc
in a loop is inefficient.– jpp
Nov 11 at 11:56
This should be the accepted answer.
pd.DataFrame.loc
in a loop is inefficient.– jpp
Nov 11 at 11:56
add a comment |
up vote
2
down vote
Why don't you try this-- not sure if it's exactly what you're looking for since you took that part out in your edit:
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
Output:
SYMBOL Volume Price OI OHLC 22SMA
0 ABB True True True False False
1
Thank you !! It was a silly question as I am new to this language :)
– Vinay Shukla
Nov 11 at 11:50
add a comment |
up vote
2
down vote
Why don't you try this-- not sure if it's exactly what you're looking for since you took that part out in your edit:
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
Output:
SYMBOL Volume Price OI OHLC 22SMA
0 ABB True True True False False
1
Thank you !! It was a silly question as I am new to this language :)
– Vinay Shukla
Nov 11 at 11:50
add a comment |
up vote
2
down vote
up vote
2
down vote
Why don't you try this-- not sure if it's exactly what you're looking for since you took that part out in your edit:
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
Output:
SYMBOL Volume Price OI OHLC 22SMA
0 ABB True True True False False
Why don't you try this-- not sure if it's exactly what you're looking for since you took that part out in your edit:
for x in range(0,numberOfRows):
data_frame.loc[x] = [SYMBOL,volume_increasing,price_increase,OI_CHANGE,closedAboveYesterday,Above_22SMA]
Output:
SYMBOL Volume Price OI OHLC 22SMA
0 ABB True True True False False
answered Nov 11 at 11:44
leeym
3121213
3121213
1
Thank you !! It was a silly question as I am new to this language :)
– Vinay Shukla
Nov 11 at 11:50
add a comment |
1
Thank you !! It was a silly question as I am new to this language :)
– Vinay Shukla
Nov 11 at 11:50
1
1
Thank you !! It was a silly question as I am new to this language :)
– Vinay Shukla
Nov 11 at 11:50
Thank you !! It was a silly question as I am new to this language :)
– Vinay Shukla
Nov 11 at 11:50
add a comment |
up vote
0
down vote
It seems to me you're not quite indexing the dataframe properly. You can either do this:
for x in range(0, numberOfRows):
data_frame['SYMBOL'][x] = SYMBOL
data_frame['Volume'][x] = volume_increasing
data_frame['Price'][x] = price_increase
data_frame['OI'][x] = OI_CHANGE
data_frame['OHLC'][x] = closedAboveYesterday
data_frame['22SMA'][x] = Above_22SMA
which will give you your desired output, alternatively you can use dictionaries and avoid the for loop altogether:
columns = ['SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA']
data = 'SYMBOL': 'AAB',
'Volume': True,
'Price': True,
'OI': True,
'OHLC': False,
'22SMA': False
data_frame = pd.DataFrame(data=data, index=np.arange(0, 1), columns=columns)
add a comment |
up vote
0
down vote
It seems to me you're not quite indexing the dataframe properly. You can either do this:
for x in range(0, numberOfRows):
data_frame['SYMBOL'][x] = SYMBOL
data_frame['Volume'][x] = volume_increasing
data_frame['Price'][x] = price_increase
data_frame['OI'][x] = OI_CHANGE
data_frame['OHLC'][x] = closedAboveYesterday
data_frame['22SMA'][x] = Above_22SMA
which will give you your desired output, alternatively you can use dictionaries and avoid the for loop altogether:
columns = ['SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA']
data = 'SYMBOL': 'AAB',
'Volume': True,
'Price': True,
'OI': True,
'OHLC': False,
'22SMA': False
data_frame = pd.DataFrame(data=data, index=np.arange(0, 1), columns=columns)
add a comment |
up vote
0
down vote
up vote
0
down vote
It seems to me you're not quite indexing the dataframe properly. You can either do this:
for x in range(0, numberOfRows):
data_frame['SYMBOL'][x] = SYMBOL
data_frame['Volume'][x] = volume_increasing
data_frame['Price'][x] = price_increase
data_frame['OI'][x] = OI_CHANGE
data_frame['OHLC'][x] = closedAboveYesterday
data_frame['22SMA'][x] = Above_22SMA
which will give you your desired output, alternatively you can use dictionaries and avoid the for loop altogether:
columns = ['SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA']
data = 'SYMBOL': 'AAB',
'Volume': True,
'Price': True,
'OI': True,
'OHLC': False,
'22SMA': False
data_frame = pd.DataFrame(data=data, index=np.arange(0, 1), columns=columns)
It seems to me you're not quite indexing the dataframe properly. You can either do this:
for x in range(0, numberOfRows):
data_frame['SYMBOL'][x] = SYMBOL
data_frame['Volume'][x] = volume_increasing
data_frame['Price'][x] = price_increase
data_frame['OI'][x] = OI_CHANGE
data_frame['OHLC'][x] = closedAboveYesterday
data_frame['22SMA'][x] = Above_22SMA
which will give you your desired output, alternatively you can use dictionaries and avoid the for loop altogether:
columns = ['SYMBOL','Volume', 'Price', 'OI','OHLC','22SMA']
data = 'SYMBOL': 'AAB',
'Volume': True,
'Price': True,
'OI': True,
'OHLC': False,
'22SMA': False
data_frame = pd.DataFrame(data=data, index=np.arange(0, 1), columns=columns)
answered Nov 11 at 12:13
J.Aluko
163
163
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248122%2fdata-frame-misaligned-using-pandas-on-python3%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What format is
data
? Can you show us in your question?– jpp
Nov 11 at 11:16
@jpp they are
boolean
andstring
if you see I have data which in the form ofvariables
which I want to organise in tabular format and convert to csv. I am trying to map them like SYMBOL "variable"– Vinay Shukla
Nov 11 at 11:18
No, I don't see... all I see is
SyntaxError
because you haven't defined any of your variables,SYMBOL
etc in your question.– jpp
Nov 11 at 11:19
Its a running code though I will edit it or create a pseudo code
– Vinay Shukla
Nov 11 at 11:20
1
Where is the data for PCJEWELLER coming from?
– leeym
Nov 11 at 11:25