python w/ pandas invalid syntax [duplicate] - python

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 3 years ago.
Python 3.4 using Pandas in beginners tutorial. Code below. Keep getting syntax error in print with carrot under the d in pd. Spent last hour trolling the web to no avail. Just starting out.
import numpy as np
import csv as csv
import pandas as pd
readdata = csv.reader(open("c:\MyData\BYLCsv.csv"))
data = []
for row in readdata:
data.append(row)
Header = data[0]
data.pop(0)
print pd.DataFrame(data, columns=Header)

You are probably using python 3 and for the print command you need the parenthesis:
print (pd.DataFrame(data, columns=Header))

Related

Pandas's .head() not working when located in Python try block [duplicate]

This question already has answers here:
IPython and REPL behave differently when displaying data without the print function
(1 answer)
How to configure IPython to execute cell blocks the same way as a plain Python REPL does?
(1 answer)
Closed 3 months ago.
Using MS VS Code and Pandas, the .head() function is working fine and printing out the DataFrame when it is not in in a try block
import pandas as pd
df = pd.read_csv("FIFA.csv")
df.head(10)
But when I use the try block like following it is not printing out the df unless I use the print function like print(data.head(10))
import pandas as pd
try:
data = pd.read_csv("FIFA.csv")
data.head(10)
except:
print("An exception occurred")
Why is this happening?

NameError: name 'nan' is not defined [duplicate]

This question already has answers here:
NaN in mapper - name 'nan' is not defined
(3 answers)
Closed 6 months ago.
After I copy/paste a list of intervals taken from a column dataframe, a 'nan' entry is included, and the list looks exactly like the following one:
from pandas import Interval
inter=[Interval(32.252, 40.21, closed='right'), Interval(40.21, 48.168, closed='right'),nan]
but if I try to print it
print(inter)
I get the following error:
NameError: name 'nan' is not defined
I tried to substitute 'nan' for 'np.nan' but it seems like that the presence of the 'nan' entry in the 'inter' list, which I repeat, I manually copied and pasted it from an existing one,
is a problem.
How should I solve this?
Python does not have a built-in name nan, nor is there a keyword.
It looks as if you forgot to import it;
numpy defines such a name:
from numpy import nan
From the local name df I infer you are probably using pandas; pandas' documentation usually uses np.nan, where np is the numpy module imported with import numpy as np.
Reference: here

How to print only part of an Excel column on pycharm? [duplicate]

This question already has answers here:
Way to read first few lines for pandas dataframe
(2 answers)
Closed 1 year ago.
Using:
import pandas as pd
x = pd.read_excel(r"C:\Users\nan\PycharmProjects\giraffe\GENERA.xlsx", engine="openpyxl")
print(x)
I can easily print the entire sheet from Excel into pycharm, but I needed only the first 5 lines of the Excel document. How can I do that?
May be you want to use pandas.DataFrame.head()
x.head(5)

Showing all rows and columns of Pandas dataframe [duplicate]

This question already has answers here:
Pandas: Setting no. of max rows
(10 answers)
Closed 1 year ago.
I am working with python 3 and the pandas package in visual studio code and I the print() function is not displaying correctly.
For example when I am using df.head() it looks good.
But If I use the print() statement I no longer see all of the columns next to each other, some of them get dragged down for some reason. And I can't see the entire data
Anyone knows what I can do to see the entire data, and all of the columns next to each other?
The problem comes from library pandas that cuts part of your dataframe when it's too long. Before your print, add this line:
pandas.set_option('max_row', None)
to display the entier row.
Also, you will be able to see all your data adding None argument in head():
trading.head(None)

how to use pandas correctly to print first five rows [duplicate]

This question already has answers here:
How to convert a Scikit-learn dataset to a Pandas dataset
(27 answers)
Closed 2 years ago.
Can someone please help me use the head() function correctly? I'm pretty sure I have to use the read_csv() function beforehand but I'm not sure how to do that as my dataset isn't .csv. I'm also not sure how to link digits to data or if I even need the data variable at all. I bolded what I know shouldn't be there.
#Load iris data from scikit-learn's datasets
from sklearn.datasets import load_iris
digits = load_iris()
#Print the first five rows of the data
import pandas as pd
data = pd.read_csv(**'foo.csv'**, header=None)
data.head()
data.head(5) should work.
more documentation here https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.head.html

Categories