How to export dataframe to csv using conditions? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am merging data using different data sets and appending them in one single dataset. The problem is that sometimes dataset df_fours is empty. To deal with this I have used try and pass statements.
Now when df_fours_unique is bypassed and when I try to export the results in CSV it gives the error:
df_append3 is not defined
What I want is to have some conditional statement (or if there is something else) which will export df_append3 if it doesn't have any error. Otherwise it will just append df_append2. What I currently have is:
df_unique = pd.merge(df7,df6_1,on='DEL_KEY1',how='left')
df_twos = pd.merge(df9,df8_1,on='DEL_KEY1',how='left')
df_twos_unique = df_twos[df_twos.index % 2 == 0]
df_threes = pd.merge(df11,df10_1,on='DEL_KEY1',how='left')
df_threes_unique = df_threes[df_threes.index % 3 == 0]
try:
df_fours = pd.merge(df13,df12_1,on='DEL_KEY1',how='left')
df_fours_unique = df_fours[df_fours.index % 4 == 0]
except:
pass
df_append1 = df_unique.append(df_twos_unique)
df_append2 = df_append1.append(df_threes_unique)
try:
df_append3 = df_append2.append(df_fours_unique)
except:
pass
df_append3.to_csv('export.csv')
Couldn't attach the datasets due to confidentiality.

What I want is to have some conditional statement (or if there is
something else) which will export df_append3 if it doesn't have any
error. Otherwise it will just append df_append2.
There is, and you're alrady using it! It's called try/except... If there was no error (inside the try) - add df_append3. Otherwise (except), append df_append2:
try:
df_append3 = df_append2.append(df_fours_unique)
df_append3.to_csv('export.csv')
except:
df_append2.to_csv('export.csv')

https://pandas.pydata.org/pandas-docs/version/0.18/generated/pandas.DataFrame.empty.html
if not df_fours.empty:
#The dataframe is not empty, we can write it
and by same token
if not df_append3.empty:
#can write this too...
Better approach than try: except: I'd think.

Related

Key Error even though key IS in dictionary? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Dictionary:
section of dictionary
My Code:
code
Error says:
3
So how come the date key works fine but for freq it fails?
ps. my first time posting, so am very sorry for the sloppy structure of the post
This can only happen when one of your day is missing the freq parameter.
Try catching the day in which the error is happening. Then manually check that particular entry.
date_list = []
frequency_list = []
try:
for i in obj:
date = obj[i]["date"]
frequency = obj[i]["freq"]
date_list.append(date)
frequency_list.append(frequency)
except:
print(i)

How to check if value is None [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to check if the "nilai" is None/Null
cursor.execute('SELECT s.kode_ktg_id,(SUM(n.nilai_angka * S.nilai_mk)/SUM(s.nilai_mk)) as nilai_ktg FROM mahasiswa_khs k, mahasiswa_convert_nilai n, mata_kuliah_si s WHERE k.nilai = n.nilai_huruf AND k.nim = "%s" AND k.kode = s.kode GROUP BY s.kode_ktg_id',[nim])
nilai = cursor.fetchall()
I check with this
if nilai[0] is None:
But I got error tuple index out of range
This is because nilai is an empty tuple, since it returned no records.
You can check if it is empty with:
if not nilai:
# no records
else:
# at least one record
That being said, in Django you can make use of the Django ORM, which is often safer, and wraps elements in model objects.

How to Avoid Duplicate Data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
while True:
if bbs_number > lately_number():
sys.stdout = open('date.txt','a')
bbs_lists = range(highest_number() +1, bbs_number +1)
for item in bbs_lists:
url_number = "url" + str(item)
try:
result = requests.get(url_number)
bs_number = BeautifulSoup(result.content, "lxml")
float_box = bs_number.find("div", {"class": "float_box"})
parameter_script = float_box
print("bs_obj()")
except AttributeError as e:
print("error")
with open('lately_number.txt', 'w') as f_last:
f_last.write(str(bbs_number))
Using the while statement above does not cause an error, but duplicate data will be output to date.txt.
I want to modify in the early stages of setting the range value, rather than removing duplicates in the later stages of typing in date.txt.
One possibility is that the existing lately_number() will output a duplicate range to date.txt, because sometimes it is not possible to enter the value correctly in the writing process of lately_number.txt.
I would be grateful if you can help me with a better function expression to add or replace.
The simplest way would be to read the date.txt into a set. Then, you can check the set to see if the date is already in there, and if it isn't, write the date to the date.txt file.
E.G.
uniqueDates = set()
#read file contents into a set.
with open("date.txt", "r") as f:
for line in f:
uniqueDates.add(line.strip()) #strip off the line ending \n
#ensure what we're writing to the date file isn't a duplicate.
with open("date.txt", "a") as f:
if("bs_obj()" not in uniqueDates):
f.write("bs_obj")
You'll probably need to adjust the logic a bit to fit your needs, but, I believe this is what you're trying to accomplish?

How can I take a value from a document and add another value to it? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a Python program I'm writing that I would like to have track statistics given inputs. I'd like it to have two documents set up, and be able to refer to each. In each, there would be a value, let's say it's x. The program would be able to generate a number, and I'd like to be able to update the number in a given document by adding the generated number. Right now, my code would be as follows:
f1 = open("player1records.txt", "a+")
f1.write(str(int(P1wins) + int(f1.read)))
This, however, raises the following:
TypeError: int() argument must be a string, a bytes-like object or a number,
not 'builtin_function_or_method'
How can I take that x and add another number to it, then update the document?
don't forget to add the () to the end of a function to call it:
f1.write(str(int(P1wins) + int(f1.read()))) # not f1.read
this sort of thing is difficult to do safely, one tends to end up with code that does:
from os import replace
def updateRecords(value, filename="player1records.txt"):
try:
with open(filename) as fd:
prev = int(fd.read())
except (FileNotFoundError, ValueError):
prev = 0
changed = prev + value
# write to a temp file in case of failure while doing this
tmpname = filename + '~'
with open(tmpname, 'w') as fd:
fd.write(str(changed))
# perform atomic rename so we don't end up with a half written file
replace(tmpname, filename)
all of this fiddling is why people tend to end up hiding this complexity behind a relational database. Python includes a relatively nice SQLite interface. if everything was set up, you'd be able to do:
with dbcon:
dbcon.execute(
"UPDATE player SET wins=wins + ? WHERE player_id = ?",
(P1wins, 1))
and have the SQLite library take care of platform specific fiddly bits…

How to add data to a list in the code while running [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Suppose i want to add something to
list = []
such that the value of list gets updated in the code it self.
In runtime:
list gets modified to
list =['hello','Dude']
How can i do so?
What i mean is, that there are real changes made to the List value in the .py file.
Judging from your comments to one of the other answers what you are looking for is a way to serialize and save an object to a file so you can reload it once you re-run the program. This is done using pickle.
An example of this can be found on stack overflow: How to save an object in Python:
import pickle
try:
with open('list.pk', 'rb') as input:
list = pickle.load(input)
except:
list = []
list.append('something')
print(list)
with open('list.pk', 'wb') as output:
pickle.dump(list, output, pickle.HIGHEST_PROTOCOL)
Just use append where ever you need it:
list = []
list.append('hello')
print list
list.append('Dude')
print list
Output:
['hello']
['hello', 'Dude']<
Easy way would be to create additional file and to store variables there.
Code:
list = []
f = open("list.txt", "r+")
for item in f:
list.append(str(item).rstrip())
f.write("Something")
f.close()
list.txt:
hello
Dude
list.txt after execution:
hello
Dude
Something
The only way to do it is rewriting the .py file.
You don't really want to do that even if it's indeed technically possible.
You should instead keep your data in a separate file, loaded when starting the program and saved back when exiting.
There are special modules for storing python values like e.g. shelve.
A very common alternative is storing the data in a format that can be understood even by other languages intead of python objects, e.g. in a relational database like sqlite.

Categories