I want to change the name of the pictures I took. It should look like this after:
20200701_Foto_1.1.jpg
20200701_Foto_1.2.jpg
20200701_Foto_1.3.jpg
20200701_Foto_PB1.jpg
20200701_Foto_2.1.jpg
20200701_Foto_2.2.jpg
20200701_Foto_2.3.jpg
20200701_Foto_PB2.jpg
...
The code written down here does not work and I don't know how to write it differently. Does someone of you have an idea ? :-)
Looking forward reading you!
import os
from natsort import natsorted
datum = 20200701
os.chdir('/Users/Aka/Desktop/'+datum+'_Fotos_rawdata Kopie')
for i in enumerate(natsorted(os.listdir('/Users/Aka/Desktop/'+datum+'_Fotos_rawdata Kopie'))):
for j in (1,4):
if j ==1 or j==2 or j==3:
os.rename(datum+"_Foto_",i+"."+j,".jpg")
if j ==4:
os.rename(datum+"_Foto_PB",i,".jpg")
Related
I have a list like below
I want to be able to compare the percent change of QQQ at 9:35 to various other stocks like AAPL and AMD at the same time. So check if percent change of AAPL at 9:35 is greater than percent change of QQQ at 9:35. Same thing for AMD at 9:35 and then at 9:40 and then 9:45 and so on.
I want to do this via python
This is what i have so far but not quite correct
import pandas as pd
import time
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr
from collections import Counter
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import os
from pandas import ExcelWriter
d1 = dt.datetime(2020, 8, 5,9,00,00)
d2 = dt.datetime(2020, 8, 5,16,00,00)
pc=Counter()
filePath=r"C:\Users\Adil\Documents\Python Test - ET\Data\Trail.xlsx"
stocklist = pd.read_excel(filePath)
for i in stocklist.index:
symbol=stocklist['Symbol'][i]
date=stocklist['Date'][i]
close=stocklist['Close'][i]
pc=stocklist['PercentChange'][i]
if (pc[i]>pc['QQQ']):
print(pc[i])
Alright,
I got from a comment, an explanation of what the OP wants:
Yes so i want to see if within a given time 5 min time period if a
stock performed better than QQQ
First thing you need to do, is make it so you can look up your information by the time and symbol. Here is how I would do that:
my_data = {}
for i in stocklist.index:
symbol=stocklist['Symbol'][i]
date=stocklist['Date'][i]
pc=stocklist['PercentChange'][i]
my_data[symbol, date] = pc
That makes a dictionary where you can lookup percent changes by calling my_data['ABCD', 'datetime']
Then, I would make a list of all the times.
time_set = set()
for i in stocklist.index:
date = stocklist['Date'][i]
time_set.add(date)
times = list(time_set)
times.sort()
If you are pressed for computer resources, you could combine those two loops and run them together, but I think having them separate makes the code easier to understand.
And then do the same thing for symbols:
sym_set = set()
for i in stocklist.index:
date = stocklist['Symbol'][i]
sym_set.add(date)
symbols = list(sym_set)
symbols.sort()
Once again, you could have made this set during the first for-loop, but this way you can see what we are trying to accomplish a bit better.
Last thing to do, is actually make the comparisons:
for i in times:
qs = my_data['QQQ', i]
for j in symbols:
if qs != 'QQQ':
which = "better" if my_data[j, i]>qs else "worse"
print(j + " did " + which + " than QQQ at " + i)
Now, this just prints the information out to the console, you should replace the print command with however you want to output it. (Also, I assumed higher was better; I hope that was right.)
I have and document that consist of many compounds (or sometimes combined) word as:
document.csv
index text
0 my first java code was helloworld
1 my cardoor is totally broken
2 I will buy a screwdriver to fix my bike
As seen above some words are combined or compound and I am using compound word splitter from here to fix this issue, however, I have trouble to apply it in each row of my document (like pandas series) and convert the document into a clean form of:
cleanDocument.csv
index text
0 my first java code was hello world
1 my car door is totally broken
2 I will buy a screw driver to fix my bike
(I am aware of word such as screwdriver should be together, but my goal is cleaning the document). If you have a better idea for splitting only combined words, please let me know.
splitter code may works as:
import pandas as pd
import splitter ## This use enchant dict (pip install enchant requires)
data = pd.read_csv('document.csv.csv')
then, it should use:
splitter.split(data) ## ???
I already looked into something like this but this not work in my case. thanks
You use apply wit axis =1 : Can you try the following
data.apply(lambda x: splitter.split(j) for j in (x.split()), axis = 1)
I do not have splitter installed on my system. By looking at the link you have provided, I have this following code. Can you try:
def handle_list(m):
ret_lst = []
L = m['text'].split()
for wrd in L:
g = splitter.split(wrd)
if g :
ret_lst.extend(g)
else:
ret_lst.append(wrd)
return ret_lst
dft.apply(handle_list, axis = 1)
As part of a school assignment on DSL and code generation, I have to translate the following program written in Python/Scikit-learn into R language (the topic of the exercise is an hypothetic Machine Learning DSL).
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import cross_validate
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
df = pd.read_csv('boston.csv', sep=',')
df.head()
y = df["medv"]
X = df.drop(columns=["medv"])
clf = DecisionTreeRegressor()
scoring = ['neg_mean_absolute_error','neg_mean_squared_error']
results = cross_validate(clf, X, y, cv=6,scoring=scoring)
print('mean_absolute_errors = '+str(results['test_neg_mean_absolute_error']))
print('mean_squared_errors = '+str(results['test_neg_mean_squared_error']))
Since I'm a perfect newbie in Machine Learning, and especially in R, I can't do it.
Could someone help me ?
Sorry for the late answer, probably you have already finished your school assignment. Of course we cannot just do it for you, you probably have to figure it out by yourself. Moreover, I don't get exactly what you need to do. But some tips are:
Read a csv file
data <-read.csv(file="name_of_the_file", header=TRUE, sep=",")
data <-as.data.frame(data)
The header=TRUE indicates that the file has one row which includes the names of the columns, the sep=',' is the same as in python (the seperator in the file is ',')
The as.data.frame makes sure that your data is kept in a dataframe format.
Add/delete a column
data<- data[,-"name_of_the_column_to_be_deleted"] #delete a column
data$name_of_column_to_be_added<- c(1:10) #add column
In order to add a column you will need to add the elements it will include. Also the # symbol indicates the beginning of a comment.
Modelling
For the modelling part I am not sure about what you want to achieve, but R offers a huge selection of algorithms to choose from (i.e. if you want to grow a tree take a look into the page https://www.statmethods.net/advstats/cart.html where it uses the following script to grow a tree
fit <- rpart(Kyphosis ~ Age + Number + Start,
method="class", data=kyphosis))
I am sure that this is a basic task and that the answer is somewhere on google but the problem I have is that I don't know what this is "called" so I am having a bad time trying to google it, almost every page demonstrates merging two lists which is not in my interest.
I basically have two lists where I would like to add the values from the list "add" to the the end of each word in the list "stuff" and print it out.
add = ['123', '12345']
stuff = ['Cars', 'Suits', 'Drinks']
Desired output
Cars123
Cars12345
Suits123
Suits12345
Drinks123
Drinks12345
Thanks in advance, and sorry again for bad research.
Is there any reason you can't just use a nested loop? It's certainly the simplest solution.
for i in stuff:
for j in add:
print(i+j)
gives
Cars123
Cars12345
Suits123
Suits12345
Drinks123
Drinks12345
This assumes that both lists are strings.
As a side point, shadowing function names like add is generally a bad idea for variables, so I would consider changing that.
Ignore what I said about combinations in the comment!
>>> from itertools import product
>>> add = ['123', '12345']
>>> stuff = ['Cars', 'Suits', 'Drinks']
>>> for a, s in product(add, stuff):
... a+s
...
'123Cars'
'123Suits'
'123Drinks'
'12345Cars'
'12345Suits'
'12345Drinks'
Addendum: Timing information: This code, which compares the nested loop with the product function from itertools does indeed show that the latter takes more time, in the ratio of about 2.64.
import timeit
def approach_1():
add = ['123', '12345']; stuff = ['Cars', 'Suits', 'Drinks']
for a in add:
for s in stuff:
a+s
def approach_2():
from itertools import product
add = ['123', '12345']; stuff = ['Cars', 'Suits', 'Drinks']
for a, s in product(add, stuff):
a+s
t1 = timeit.timeit('approach_1()','from __main__ import approach_1', number=10000)
t2 = timeit.timeit('approach_2()','from __main__ import approach_2', number=10000)
print (t2/t1)
You need two for loops for that:
for stuff_element in stuff:
for add_element in add:
print(stuff_element+add_elements)
Try this :
for i in stuff:
for j in add:
print(i+j)
Let me know if it works
I have a quick question. My code looks like below:
import quandl
names_of_company = ['KGHM','INDYKPOL','KRUK','KRUSZWICA']
for names in names_of_company:
x = quandl.get('WSE/{names_of_company}', start_date='2018-11-26',
end_date='2018-11-29')
I am trying to get all the data in one output but I can't change the names of each company one after another. Do you have any ideas?
Thanks for help
unless I'm missing something, looks like you should just be able to do a pretty basic for loop. it was the syntax that was was incorrect.
import quandl
import pandas as pd
names_of_company = ['KGHM','INDYKPOL','KRUK','KRUSZWICA']
results = pd.DataFrame()
for names in names_of_company:
x = quandl.get('WSE/%s' %names, start_date='2018-11-26',
end_date='2018-11-29')
x['company'] = names
results = results.append(x).reset_index(drop=True)