dmap = {0:'Mon',1:'Tue',2:'Wed',3:'Thur',4:'Fri',5:'Sat',6:'Sun'}
df['DayOfWeek']=df['DayOfWeek'].map(dmap)
df.head()
DayOfWeek column value is Showing as NaN rather then String
I didn't understand your question exactly but i guess it has to be something like that
dmap = {0:'Mon',1:'Tue',2:'Wed',3:'Thur',4:'Fri',5:'Sat',6:'Sun'}
count = 0
df = pd.DataFrame({'DayNumber':[], 'DayOfWeek': []})
for key, value in dmap.items() :
df.loc[count] = [str(key) , value]
count = count + 1
print(df.head())
Related
This code works perfectly but its slow like turtle. Generally I don't take this approach but I was not able to find any other option.
Well my requirement is to where ever in the column code finds 1 get the key and value of the that index defined range and go to To sheet find the key and paste the value in the required columns.
If I can jump to cells where 1 is in selected column and get the index to find the key and value and then jump to To page key column where key is instead of going to every cell and checking for it whether it is there or not.
I am new to app script and little help would be great.
Thank you in advance.
function Data_Update(){
//assigning sheet name to variables
var ss = SpreadsheetApp.getActive()
var from = ss.getSheetByName("From Sheet")//update here from sheet name(Updated)
var To= ss.getSheetByName("To Sheet")//update here to sheet name(Updated)
// Creating Loop
for (var i = 4;i<=7000;i++){//Update here from which row to start
//assigning values to check if there is 1 in column K
var udaterang = from .getRange("K"+i).getValue()//update here from which column to check for
Logger.log(i)
// Checking condition if the value is diffrent from the value already is
if (udaterang == 1) {
//creating key to find the value
var name1 = from .getRange("A"+i).getValue()//update here the key column 1
var name2 = from .getRange("B"+i).getValue()//update here the key column 2
var name3 = from .getRange("C"+i).getValue()//update here the key column 3
var name = name1.trim()+name2.trim()+name3.trim()
var rng = from .getSheetValues(i,4,1,7)//start row, start column, # rows, # columns
// Looping through each cell to check if the data needs update
for(var j=2;j<=12500;j++){
var key = To.getRange("AP"+j).getValue()
if(key == name){ //[1] because column B
To.getRange("AI"+j+":"+"AO"+j).setValues(rng)
break
}
}
}
}
}
Try this:
function Data_Update() {
const ss = SpreadsheetApp.getActive();
const fsh = ss.getSheetByName("From Sheet");
const fkvs = fsh.getRange(4, 11, fsh.getLastRow() - 3).getValues().flat();
const fvs = fsh.getRange(4, 1, fsh.getLastRow() - 3, fsh.getLastColumn()).getValues();
const tapvs = tsh.getRange(2, 42, tsh.getLastRow() - 1).getValues().flat();
fkvs.forEach((k, i) => {
let udaterang = k;
if (k == 1) {
let name = fvs[i][0].toString().trim() + fvs[i][1].toString().trim() + fvs[i][2].toString().trim();
let idx = tapvs.indexOf(name);
if (~idx) {
tsh.getRange(idx + 2, 35, 1, 7).setValues(tsh.getRange(i + 2, 4, 1, 7).getValues());
}
}
});
}
This may take some tweaking because I have not tested this as I don't have the data to do so.
I'm trying to make a script that replaces information in a template document (a contract). I've had it working looking through the columns of a google spreadsheet, but when trying to rewrite it to make it look through the rows of the spreadsheet instead of the columns it does not seem to work. It only returns information contained in the first row. I suspect it might have to do with the for-loop.
Here's a snippet of the code:
function lagkontrakt() {
const docFile = DriveApp.getFileById("DOC_FILE_ID");
const tempFolder = DriveApp.getFolderById("TEMP_FOLDER_ID");
const pdfFolder = DriveApp.getFolderById("PDF_FOLDER_ID");
var date = Utilities.formatDate(new Date(), "GMT+1", "dd/MM/yyyy");
//copies the template//
let copyFile = DriveApp.getFileById("FILE_ID").makeCopy(tempFolder);
let copyId = copyFile.getId();
let copyDoc = DocumentApp.openById(copyId);
//fetches the content of the template//
let copyBody = copyDoc.getBody();
let copyHeader = copyDoc.getHeader();
//defines active sheet//
let activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEETNAME");
let numOfRow = activeSheet.getLastRow();
//getRange(row, column, numRows, numColumns)//
let activeCol = activeSheet.getRange(1,2,numOfRow,1).getValues();
let headerCol = activeSheet.getRange(1,1,numOfRow,1).getValues();
let rowIndex = 0
//search loop//
for (; rowIndex < headerCol[0].length; rowIndex++){
copyBody.replaceText('{' + headerCol[rowIndex][0] + '}', activeCol[rowIndex][0]);
copyBody.replaceText("{dato}", date);
copyHeader.replaceText('{' + headerCol[rowIndex][0] + '}', activeCol[rowIndex][0]);
}
copyDoc.saveAndClose();
}
This is the column-version of the same script, that works just fine:
let numOfCol = activeSheet.getLastColumn();
let activeRow = activeSheet.getRange(2,1,1,numOfCol).getValues();
let headerRow = activeSheet.getRange(1,1,1,numOfCol).getValues();
let columnIndex = 0
for (; columnIndex < headerRow[0].length; columnIndex++){
copyBody.replaceText('{' + headerRow[0][columnIndex] + '}', activeRow[0][columnIndex]);
copyBody.replaceText('{dato}', date);
copyHeader.replaceText('{' + headerRow[0][columnIndex] + '}', activeRow[0][columnIndex]);
}
It looks like you have the variable in column A and the value in column B. Your For loop currently is limiting the rows that it searches to the length of the first row, which is to say that it will always be less than 1.
You need to change:
for (; rowIndex < headerCol[0].length; rowIndex++){
To:
for (; rowIndex < headerCol.length; rowIndex++){
If I'm not correct about how your data is laid out then you have reversed the array reference in a couple places and got an entire column instead of the entire row.
I have the following dataset:
Movies : moviename, genre1, genre2, genre3 ..... genre19
(All the genres above have values 0 or 1, 1 indicates that the movie is of that genre)
Now i want to find which movie(s) has least genre?
I tried the below Pig script:
items = load 'path' using PigStorage('|') as (mName:chararray,g1:int,g2:int,g3:int,g4:int,g5:int,g6:int,g7:int,g8:int,g9:int,g10:int,g11:int,g12:int,g13:int,g14:int,g15:int,g16:int,g17:int,g18:int,g19:int);
sumGenre = foreach items generate mName, g1+g2+g3+g4+g5+g6+g7+g8+g9+g10+g11+g12+g13+g14+g15+g16+g17+g18+g19 as sumOfGenres;
groupAll = group sumGenre All;
In the next step by using MIN(sumGenre.sumofGenres), i can get a genre which is the MIN value , but what am looking for is to get a moviename which has the least no. of genres, alongside the number of genres of that movie.
Can someone please help?
1. I want to know is there any other easy way to get the sum of g1+g2+...g19?
2. Also the output : movie(s) that has the least genre?
After the groupAll
r1 = minGenre = foreach groupAll generate MIN(sumGenre.sumOfGenres) as minG;
do left outer join between r1 by minG with sumGenre by sumOfGenres;
to get the list of movies having least genre..
Hope this will help..
for dynamic row field sum u can use UDF like this..
public class DynRowSum extends EvalFunc<Integer>
{
public Integer exec(Tuple v) throws IOException
{
List<Object> olist = v.getAll();
int sum = 0;
int cnt=0;
for( Object o : olist){
cnt++;
if (cnt!=1) {
int val= (Integer)o;
sum = sum + val;
}
}
return new Integer(sum);
}
}
In pig update the script like this..
grunt>sumGenre = foreach items generate mName,DynRowSum(*) as sumOfGenres;
Advantage here you will get if genre increase or decrease code will remain same..
a = LOAD 'path';
b = FOREACH a generate FLATTEN(STRSPLIT($0, '\\|'));
c = FOREACH b generate $0 as movie, FLATTEN(TOBAG(*)) as genre;
d = FILTER c BY movie!=genre;
e = GROUP d BY $0;
f = FOREACH e GENERATE group, SUM(d);
i = ORDER f BY $1;
j = LIMIT i 1;
I encountered the following problem in spark:
...
while(...){
key = filtersIterator.next()
pricesOverLeadTimesKeyFiltered = pricesOverLeadTimesFilteredMap_cached
.filter(x => x._1.equals(key))
.values
resultSRB = processLinearBreakevens(pricesOverLeadTimesKeyFiltered)
resultsSRB = resultsSRB.union(resultSRB)
}
....
By this way, I accumulate the same resultSRB in resultsSRB.
But here are "some" tricks allowing me to add a different/right resultSRB for each iteration
call resultSRB.collect() or resultSRB.foreach(println) or println(resultSRB.count) after each processLinearBreakevens(..) call
perform the same operation on pricesOverLeadTimesKeyFiltered at the beginning of processLinearBreakevens(..)
It seems I need to ensure that all operations must be "flushed" before performing the union. I already tried the union through a temporary variable, or to persist resultSRB, or to persist pricesOverLeadTimesKeyFiltered but still the same problem.
Could you help me please?
Michael
If my assumption is correct; that all of these are var, then the problem is closures. key needs to be a val as it is being lazily captured into your filter. So, when it is finally acted on, the filtering is always using the last state of key
My example:
def process(filtered : RDD[Int]) = filtered.map(x=> x+1)
var i = 1
var key = 1
var filtered = sc.parallelize(List[Int]())
var result = sc.parallelize(List[Int]())
var results = sc.parallelize(List[Int]())
val cached = sc.parallelize(1 to 1000).map(x=>(x, x)).persist
while(i <= 3){
key = i * 10
val filtered = cached
.filter(x => x._1.equals(key))
.values
val result = process(filtered)
results = results.union(result)
i = i + 1
}
results.collect
//expect 11,21,31 but get 31, 31, 31
To fix it, change key to be val in the while loop and will get your expected 11,21,31
I am using LINQ to SQL (through LINQPad) to extract some data from a MySQL database.
var res = MyTable.First();
res.Dump();
And I have 255 columns like so.
i0 | 0.01
i1 | 0.11
i2 | 4.01
...
i254 | 1.12
I would like to convert the values from the 255 columns into an array after the select. Is there a way to iterate through the value of res as a dictionary so it will be possible to make a loop like (in pseudo code):
for i in 0..254 do
array[i] = res["i" + i]
As suggested by #sloth I got it to work with reflection as so:
var type = res.GetType();
type.GetMembers().Where(s => s.Name.StartsWith("i")).Select(s => new { Name = s.Name, Value=s.GetValue(res)}).Dump();
You could use reflection the get the properties.
Example:
var res = MyTable.First();
var type = res.GetType();
for(int i=0; i<=254; i++)
array[i] = type.GetProperty("i" + i).GetValue(res);