This question already has an answer here:
Pandas to_csv call is prepending a comma
(1 answer)
Closed 1 year ago.
I am trying to do math on a specific cell in a csv file with pandas, but whenever I try to do it, it adds a column on the left that looks like
,
1,value,value
2,value,value
3,value,value
4,value,value
5,value,value
So if I run it multiple times it looks like
,
1,1,1,value,value
2,2,2,value,value
3,3,3,value,value
4,4,4,value,value
5,5,5,value,value
How do I make it not do this??
My code is: add_e_trial = equations_reader.at[bank_indexer_addbalance, 1] = reciever_amount
Whenever you do a df.to_csv, include index=False. The left most column is the index of the dataset
df.to_csv(index=False)
This question already has an answer here:
Karate - match fails for inline array
(1 answer)
Closed 1 year ago.
Usually matching works just fine but I've had weird random matching errors recently :
$.userInfo | not equal | match failed for name: 'phone' (MAP:MAP)
{"givenName":"##string","familyName":"##string","phone":"##string"}
{"givenName":"Andreas","familyName":"Conroy","phone":"+19515554384"}
$.userInfo.phone | not equal (STRING:STRING)
'##string'
'+19515554384'
How is '+19515554384' not a string ?
Thanks
Sounds like you have your "expected" vs "actual" data mixed up. Here below are 2 lines you can cut and paste and verify that it works:
* def response = {"givenName":"Andreas","familyName":"Conroy","phone":"+19515554384"}
* match response == {"givenName":"##string","familyName":"##string","phone":"##string"}
This question already has answers here:
Conditional Replace Pandas
(7 answers)
Adding a new pandas column with mapped value from a dictionary [duplicate]
(1 answer)
Closed 3 years ago.
This is my dataset :
Datset example
I want to assign to the different values* such as 'W M I HOLDINGS CORP' and 'MILESTONE SCIENTIFIC INC' another variable in the column 'ticker' in order to have the opportunity to sort them.
In the column 'ticker' I need to add WMIH and WLSS respectively to the two different values.
How can I do that ?
I am going to expect the output with something like this :
Output Example
You should be ok with this. Assuming you have your dataset in df variable.
df.loc[df['comnam'] == 'W M I HOLDINGS CORP', 'ticker'] = 'WMIH'
df.loc[df['comnam'] == 'MILESTONE SCIENTIFIC INC', 'ticker'] = 'WLSS'
This question already has an answer here:
Convert pandas._period.Period type Column names to Lowercase
(1 answer)
Closed 4 years ago.
I have a dataframe where I used
df.groupby(pd.PeriodIndex(df.columns, freq='Q'), axis=1).mean() to combine all column names from month into quarter by taking the mean.
However, the result dataframe has columns like below and I could not change all upper case Q into lower case 'q'.
PeriodIndex(['2000Q1', '2000Q2', '2000Q3', '2000Q4', '2001Q1', '2001Q2',
'2001Q3', '2001Q4', '2002Q1', '2002Q2', '2002Q3', '2002Q4',
'2003Q1', '2003Q2', '2003Q3', '2003Q4', '2004Q1', '2004Q2',
'2004Q3', '2004Q4', '2005Q1', '2005Q2', '2005Q3', '2005Q4',
'2006Q1', '2006Q2', '2006Q3', '2006Q4', '2007Q1', '2007Q2',
'2007Q3', '2007Q4', '2008Q1', '2008Q2', '2008Q3', '2008Q4',
'2009Q1', '2009Q2', '2009Q3', '2009Q4', '2010Q1', '2010Q2',
'2010Q3', '2010Q4', '2011Q1', '2011Q2', '2011Q3', '2011Q4',
'2012Q1', '2012Q2', '2012Q3', '2012Q4', '2013Q1', '2013Q2',
'2013Q3', '2013Q4', '2014Q1', '2014Q2', '2014Q3', '2014Q4',
'2015Q1', '2015Q2', '2015Q3', '2015Q4', '2016Q1', '2016Q2',
'2016Q3'],
dtype='period[Q-DEC]', freq='Q-DEC')
I have tried using df.columns=[x.lower() for x in df.columns] and it gives an
error:'Period' object has no attribute 'lower'
This looks like a duplicate of the issue posted here: Convert pandas._period.Period type Column names to Lowercase
Basically, you'll want to reformat the Period output to have a lowercase q like so:
df.columns = df.columns.strftime('%Yq%q')
Alternatively, if you want to modify your PeriodIndex object directly, you can do something like:
# get the PeriodIndex object you pasted in your question
periods = df.groupby(pd.PeriodIndex(df.columns, freq='Q'), axis=1).mean()
# format the entries accordingly
periods = [p.strftime('%Yq%q') for p in periods]
The %Y denotes the year format, the first q is the lowercase "q" you want, and the %q is the quartile.
Here is the documentation for a Period's strftime() method, which returns the formatted time string. At the bottom they have some nice examples!
Looking at the methods listed in the Pandas documentation, lower() isn't an available method for the Period object, which is why you're getting this error (a PeriodIndex is just an array of Periods, which denote a chunk of time).
Ok so I am trying to reference one variable with another in SQL.
X= a,b,c,d (x is a string variable with a list of things in it)
Y= b ( Y is a string variable that may or may not have a vaue that appears in X)
I tried this:
Case when Y in (X) then 1 else 0 end as aa
But it doesnt work since it looks for exact matches between X and Y
also tried this:
where contains(X,#Y)
but i cant create Y globally since it is a variable that changes in each row of the table.( x also changes)
A solution in SAS would also be useful.
Thanks
Maybe like will help
select
*
from
t
where
X like ('%'+Y+'%')
or
select
case when (X like ('%'+Y+'%')) then 1 else 0 end
from
t
SQLFiddle example
In SAS I would use the INDEX function, either in a data step or proc sql. This returns the position within the string in which it finds the character(s), or zero if there is no match. Therefore a test if the value returned is greater than zero will result in a binary 1:0 output. You need to use the compress function with the variable containing the search characters as SAS pads the value with blanks.
Data step solution :
aa=index(x,compress(y))>0;
Proc Sql solution :
index(x,compress(y))>0 as aa