if the column hotel_name values already exist then add 1 in the column of is_pool,otherwise add new data to hotel_name column - python-3.x

I have 4 columns hotel_name, is_pool ,is_wifi ,is_gym.First time when i start the loop 10 hotels will added in hotel_name column and 1 is added to is_pool column. Second time when the loop starts for wifi it will check if the hotels already exist in hotel_name column then it will add 1 in is_spa(infront of that hotel which already exist like avari-hotel in below example) ,if these hotels not exist then it will add new hotel in hotel_name column and add 1 in is_wifi column same for is_gym etc...
Hotel_name Is_pool Is_wifi Is_gym
Grand_hayat 1 0 0
Royal-marria 1 0 0
Peart-continent 1 0 0
Sub-hotelways 1 0 0
Grand_marqs 1 0 0
Avari hotels 1 1 0
Chenone hotels 1 0 0
Savoey hotels 1 0 0
The grand 1 0 0
Hotel-range 1 0 0
Sub-marry 0 1 0
Royal-reside 0 1 0
Xyz 0 1 0
Abc 0 1 0
. . . .
. . . .
how i achieve this task kindly help :) thanks in advance
CREATE TABLE "Hotels" (
`hotel_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`hotel_name` TEXT NOT NULL,
`is_pool` INTEGER DEFAULT 0,
`is_wifi` INTEGER DEFAULT 0,
`is_gym` INTEGER DEFAULT 0,
)
if(prefrence[i]=='pool'):
c.execute("INSERT INTO hotels (Hotel_name,is_pool) VALUES (?,?)" , [hotel], 1)

Just try to update the row. If the row was not found, insert it:
c.execute("UPDATE hotels SET is_wifi = 1 WHERE hotel_name = ?", [hotel])
if c.rowcount == 0:
c.execute("INSERT INTO hotels..."...)

Related

Returning column header corresponding to matched value

need some help here.. I am looking to retrieve Gender from Sheet 2 corresponding to the name in Sheet 1.
Step 1 - Match the name in sheet 1 to sheet 2 (not all names in sheet 1 will be in sheet 2, mark NA for non matching names)
Step 2 - Look for the corresponding gender in sheet 2.
Step 3 - Retrieve the column header or the last number in the column header (1,2,3...6)
Sheet 1
Name
Gender
w
???
e
r
t
y
u
i
q
w
e
r
Sheet 2
Name
Male 1
Female 2
other 3
other 4
other 5
Do not know 6
w
1
0
0
0
0
0
a
0
0
0
0
0
1
q
1
0
0
0
0
0
r
0
1
0
0
0
0
e
1
0
0
0
0
0
t
0
0
0
0
1
0
y
0
0
0
0
0
1
u
0
1
0
0
0
0
with Office 365 we can use FILTER:
=IFERROR(FILTER($F$1:$K$1,INDEX($F$2:$K$9,MATCH(A2,$E$2:$E$9,0),0)=1),"No Match")
With older versions we can use another INDEX/MATCH:
=IFERROR(INDEX($F$1:$K$1,MATCH(1,INDEX($F$2:$K$9,MATCH(A2,$E$2:$E$9,0),0),0)),"No Match")

How to return all rows that have equal number of values of 0 and 1?

I have dataframe that has 50 columns each column have either 0 or 1. How do I return all rows that have an equal (tie) in the number of 0 and 1 (25 "0" and 25 "1").
An example on a 4 columns:
A B C D
1 1 0 0
1 1 1 0
1 0 1 0
0 0 0 0
based on the above example it should return the first and the third row.
A B C D
1 1 0 0
1 0 1 0
Because you have four columns, we assume you must have atleast two sets of 1 in a row. So, please try
df[df.mean(1).eq(0.5)]

pandas assign value in multiple columns based on value in one

I have a dataset like this,
sample = {'Theme': ['never give a ten','interaction speed','no feedback,premium'],
'cat1': [0,0,0],
'cat2': [0,0,0],
'cat3': [0,0,0],
'cat4': [0,0,0]
}
pd.DataFrame(sample,columns = ['Theme','cat1','cat2','cat3','cat4'])
Theme cat1 cat2 cat3 cat4
0 never give a ten 0 0 0 0
1 interaction speed 0 0 0 0
2 no feedback,premium 0 0 0 0
Now, I need to replace the values in cat columns based on value in Theme. If the Theme column has 'never give a ten', then change cat1 as 1, similarly if the theme column has 'interaction speed', then change cat2 as 1, if the theme column has 'no feedback' in it, change 'cat3' as 1 and for 'premium' change cat4 as 1.
In this sample I have provided 4 categories, I have in total 21 categories. I can do if word in string 21 times for 21 categories, but I am looking for an efficient way to write this in a function, loop every row and go through the logic and update the corresponding columns, can anyone help please?
Thanks in advance.
Here is possible set columns names by categories with Series.str.get_dummies - columns names are sorted:
df1 = df['Theme'].str.get_dummies(',')
print (df1)
interaction speed never give a ten no feedback premium
0 0 1 0 0
1 1 0 0 0
2 0 0 1 1
If need first column in output add DataFrame.join:
df11 = df[['Theme']].join(df['Theme'].str.get_dummies(','))
print (df11)
Theme interaction speed never give a ten no feedback \
0 never give a ten 0 1 0
1 interaction speed 1 0 0
2 no feedback,premium 0 0 1
premium
0 0
1 0
2 1
If order of columns is important add DataFrame.reindex:
#removed posible duplicates with remain ordering
cols = dict.fromkeys([y for x in df['Theme'] for y in x.split(',')]).keys()
df2 = df['Theme'].str.get_dummies(',').reindex(cols, axis=1)
print (df2)
never give a ten interaction speed no feedback premium
0 1 0 0 0
1 0 1 0 0
2 0 0 1 1
cols = dict.fromkeys([y for x in df['Theme'] for y in x.split(',')]).keys()
df2 = df[['Theme']].join(df['Theme'].str.get_dummies(',').reindex(cols, axis=1))
print (df2)
Theme never give a ten interaction speed no feedback \
0 never give a ten 1 0 0
1 interaction speed 0 1 0
2 no feedback,premium 0 0 1
premium
0 0
1 0
2 1

Find a subset of rows (N rows) in a Pandas data frame having the same values at a subset of columns

I have a df which contains customer data without a primary key. The same customer might show up multiple times.
I have a field (df2['campaign']) that is an int and reflects how many times the customer shows up in the df. There are also many customer attributes.
In my example, going from top to bottom, for each row (i.e. customer), I would like to find all n rows (i.e. all n customers) whose values of the education and default columns are the same. Remember n is the int contained in df2['campaign']
So as shown below, for row 0 and 1 I should search 1 row but find nothing because there are no matching values for education-default combinations.
For row 2 I should search 1 row (because campaign == 1) where education-default values match, and find 1 row in index 4.
df2.head()
job marital education default campaign housing loan contact
0 3 1 0 0 1 0 0 1
1 7 1 3 1 1 0 0 1
2 7 1 3 0 1 2 0 1
3 0 1 1 0 1 0 0 1
4 7 1 3 0 1 0 2 1
Use df2_sorted = df2.sort(['education', 'default'], ascending=[1, 1]).
Then if your data is not noisy, the rows should become neighbors.

How to change value 0 to not valid and 1 to valid in excel

Sorry for bad english,
I've some cell with 0 value and 1 value in my microsoft excel, and i want to show 0 values with not valid and 1 values with valid without affecting the formula.
My current excel :
x A B C D E F
1 1 0 0 0 1 0
2 0 1 1 0 0 1
3 1 0 1 0 0 1
4 0 1 1 1 1 1
5 0 0 1 0 1 0
What i want :
valid notvalid notvalid notvalid valid notvalid
0 1 1 0 0 1
1 0 1 0 0 1
0 1 1 1 1 1
0 0 1 0 1 0
Use a custom number format (ctrl+1) of [Color13][=1]v\ali\d;[Color9][=0]\notv\ali\d;; on the cells.
In addition to the valid/notvalid display text, I've added dark blue font for the valids and dark red for notvalid.
Considering you are now working in Worksheet1, if you don't want to edit the formula you currently have in cells A1:F5, you can:
either go to/create Worksheet2 and select the cell A1 OR select the cell A7 in Worksheet1.
write in Worksheet1!A7 or Worksheet2!A1 the following formula:
=IF(Worksheet1!A1=1,"valid","notvalid")
copy the formula dragging the fill handle as needed.
I hope I understood well what you would like to do.

Resources