How can I convert my code into a single line? [duplicate] - python-3.x

This question already has answers here:
How can I use a conditional expression (expression with if and else) in a list comprehension? [duplicate]
(6 answers)
Closed 1 year ago.
I have the following code which I would like to convert into a single line I guess using list comprehension? But I have been unsuccessful in converting it.
exp_days = ["16/04/2021","23/04/2021","27/04/2021"]
for i in range(len(df)):
if df["Date"][i] in exp_days:
list_of_days.append(1)
else:
list_of_days.append(0)
My dataframe:
Date
16/04/2021
19/04/2021
20/04/2021
21/04/2021
22/04/2021
23/04/2021
26/04/2021
27/04/2021
Expected output:
list_of_days = [1,0,0,0,0,1,0,1]

list_of_days = [ 1 if df["Date"][i] in exp_days else 0 for i in range(len(df)) ]

Alternative via numpy -
exp_days = ["16/04/2021","23/04/2021","27/04/2021"]
import numpy as np
result = np.where(df['Date'].isin(exp_days),1,0)

Related

Write a function that returns the count of the unique answers to all of the questions in a dataset [duplicate]

This question already has answers here:
How to filter Pandas dataframe using 'in' and 'not in' like in SQL
(11 answers)
How to test if a string contains one of the substrings in a list, in pandas?
(4 answers)
Closed 1 year ago.
For example, after filtering the entire dataset to only questions containing the word "King", we could then find all of the unique answers to those questions.
I filtered by using the following code:
`def lower1(x):
x.lower()
filter_dataset = lambda x:all(x) in jeopardy.Question.apply(lower1)
print(filter_dataset(['King','England']))`
The above code is printing True instead of printing the rows of jeopardy['Question'] with the keywords 'King' and 'England'.
That is the first problem.
Now I want to count the unique answers to the jeopardy['Question']
Here is the sample data frame
Now I want to create a function that does the count of the unique answers.
I wrote the following code:
`def unique_counts():
print(jeopardy['Answer'].unique().value_counts())
unique_counts()`
Which is giving me the following error:
AttributeError: 'numpy.ndarray' object has no attribute 'value_counts'
Use Series.str.contains:
jeopardy[jeopardy['Question'].str.contains('|'.join(['King','England']))]

How to split python3 List? [duplicate]

This question already has answers here:
How to extract the n-th elements from a list of tuples
(8 answers)
Closed 3 years ago.
I have this list:
[('5.333333333333333', 'n04'), ('5.0', 'n01'), ('3.9936507936507932', 'n03'), ('2.4206349206349205', 'n05'), ('1.9629629629629628', 'n02')]
and I like to have the list like this:
[n04, n01, n03, n02, n04]
how to do it? I have spend too many houres on this problem.
Help please!
You can use a list comprension to iterate over the list and pick out the values you are interested in and put them in a new list
my_list = [('5.333333333333333', 'n04'), ('5.0', 'n01'), ('3.9936507936507932', 'n03'), ('2.4206349206349205', 'n05'), ('1.9629629629629628', 'n02')]
my_new = [item[1] for item in my_list]
print(my_new)
OUTPUT
['n04', 'n01', 'n03', 'n05', 'n02']
Try:
x,y=zip(*[('5.333333333333333', 'n04'), ('5.0', 'n01'), ('3.9936507936507932', 'n03'), ('2.4206349206349205', 'n05'), ('1.9629629629629628', 'n02')])
y=list(y)
print(y)
Outputs:
['n04', 'n01', 'n03', 'n05', 'n02']

How to delete an element by index from a numpy array in Python 3? [duplicate]

This question already has answers here:
How to remove specific elements in a numpy array
(13 answers)
Closed 3 years ago.
I want to delete an element from a numpy array by index.
The commands
arr = np.linspace(-5,5,10)
del arr[0]
The code above throws an error saying cannot delete array elements.
Using pop doesn't work either. What should I do?
You should use np.delete for it.
arr = np.linspace(-5,5,10)
arr = np.delete(arr, 0)

How to pass custom function to dataframe.apply with parameters other than row(axis=1) [duplicate]

This question already has answers here:
python pandas: apply a function with arguments to a series
(7 answers)
Closed 4 years ago.
I'm working on script that automatically generates full file path column using df.apply() as below.
def generate_filepath(row):
all_files = os.listdir(files_dir)
if row['FileName'] not in all_files:
return None
value = os.path.join(files_dir, row['FileName'])
return value
csv_df['FilePath'] = csv_df.apply(generate_filepath, axis=1)
I had to declare files_dir as a global variable and then use it in the function. Is there any other I can pass it as an argument along with df.apply?
Kindly help me with good suggestions
Why don't you curry your function?
https://www.python-course.eu/currying_in_python.php
or add another argument?
Edit: Jon Clements answer is better than mine

How do I parse one character from Python Pandas String? [duplicate]

This question already has answers here:
Pandas: get second character of the string, from every row
(2 answers)
Closed 4 years ago.
I have a data frame and want to parse the 9th character into a second column. I'm missing the syntax somewhere though.
#develop the data
df = pd.DataFrame(columns = ["vin"], data = ['LHJLC79U58B001633','SZC84294845693987','LFGTCKPA665700387','L8YTCKPV49Y010001',
'LJ4TCBPV27Y010217','LFGTCKPM481006270','LFGTCKPM581004253','LTBPN8J00DC003107',
'1A9LPEER3FC596536','1A9LREAR5FC596814','1A9LKEER2GC596611','1A9L0EAH9C596099',
'22A000018'])
df['manufacturer'] = ['A','A','A','A','B','B','B','B','B','C','C','D','D']
def check_digit(df):
df['check_digit'] = df['vin'][8]
print(df['checkdigit'])]
For some reason, this puts the 8th row VIN in every line.
In your code doing this:
df['check_digit'] = df['vin'][8]
Is only selecting the 8th element in the column 'vin'. Try this instead:
for i in range(len(df['vin'])):
df['check_digit'] = df['vin'][i][8]
As a rule of thumb, whenever you are stuck, simply check the type of the variable returned. It solves a lot of small problems.
EDIT: As pointed out by #Georgy in the comment, using a loop wouldn't be pythonic and a more efficient way of solving this would be :
df['check_digit'] = df['vin'].str[8]
The .str does the trick. For future reference on that, I think you would find this helpful.
The correct way is:
def check_digit(df):
df['check_digit'] = df['vin'].str[8]
print(df)

Resources