Listado de la etiqueta: Method


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

import pandas as pd
data = pd.DataFrame({
  ‘Name’: [‘A’, ‘B’, ‘C’, ‘D’],
  ‘Roll number’: [25, 45, 23, 32],
  ‘House’: [‘Blue’, ‘Green’, ‘Green’, ‘Yellow’]
})
 
 
df = pd.DataFrame({
  ‘Name’: [‘A’, ‘B’, ‘C’, ‘D’],
  ‘Roll number’: [25, 45, 23, 32],
  ‘House’: [‘Blue’, ‘Green’, ‘Green’, ‘Yellow’]
})
 
result = data.isin(df)
print(result)
 
print()
 
df = pd.DataFrame({
  ‘Name’: [‘A’, ‘B’, ‘C’, ‘D’],
  ‘Roll number’: [25, 45, 23, 32],
  ‘House’: [‘Blue’, ‘Green’, ‘Green’, ‘Yellow’]
})
 
result = data.isin(df)
print(result)



Source link


We’ll learn about the PySpark library in this session. It is a general-purpose, in-memory, distributed processing engine that lets you effectively manage the data across several workstations.  We’ll also learn about the PySpark fillna() method that is used to fill the null values in the dataframe with a custom value, along with its examples.

What is PySpark?

PySpark is one of Spark’s supported languages. Spark is a large data processing technology that can handle data on a petabyte scale. PySpark is an Apache Spark and Python cooperation. Python is a modern high-level programming language, whereas Apache Spark is an open-source that focuses on computational tasks of clusters and mainly targets speed, ease of use, and streaming analytics. Because Spark is mostly built in Scala, creating Spark apps in Scala or Java allows you to access more of its capabilities than writing Spark programmes in Python or R. PySpark, for example, does not currently support Dataset. You may develop Spark applications to process data and launch them on the Spark platform using PySpark. The AWS offers the managed EMR and the Spark platform.

If you’re doing a data science, PySpark is a better option than Scala because there are many popular data science libraries written in Python such as NumPy, TensorFlow, and Scikit-learn. You may use PySpark  to process the data and establish an EMR cluster on AWS. PySpark can read the data from a variety of file formats including csv, parquet, json, as well as databases. For smaller datasets, Pandas is utilized, whereas for bigger datasets, PySpark  is employed. In comparison to PySpark, Pandas gives quicker results. Depending on memory availability and data size, you may switch between PySpark  and Pandas to improve performance. Always use Pandas over PySpark  when the data to be processed is enough for the memory. Spark has quickly become the industry’s preferred technology for data processing. It is, however, not the first. Before Spark, the processing engine was MapReduce.

What is PySpark Fillna()?

PySpark fillna() is a PySpark method used to replace the null values in a single or many columns in a PySpark data frame model. Depending on the business requirements, this value might be anything. It can be 0 or an empty string and any constant fiel. This fillna() method is useful for data analysis since it eliminates null values which can cause difficulties with data analysis.

Example of Using Fillna()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

from pyspark.sql import SparkSession
spark_session = SparkSession.builder
    .master(‘específico[1]’)
    .appName(‘Example’)
    .getOrCreate()

df = spark_session.createDataFrame(
    [
       (1, ‘Canada’, ‘Toronto’, None),
       (2, ‘Japan’, ‘Tokyo’, 8000000),
       (3, ‘India’, ‘Amritsar’, None),
       (4, ‘Turkey’, ‘Ankara’, 550000),
    ],
    [‘id’, ‘country’, ‘city’, ‘population’]
)
df.show()

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

+—+———+————–+———–+

| id|  country|         city | population|

+—+———+————–+———–+

1|       Canada|   Toronto|       null|

2|       Japan|      Tokyo|    8000000|

3|       India|   Amritsar|       null|

4|       Turkey|    Ankara|     550000|

+—+———+————–+———–+

We may now use merely the value argument to replace all the null values in a DataFrame:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

df.na.fill(value=0).show()

df.na.fill(value=0,subset=[«population»]).show()

df.fillna(value=0).show()

+—+———+————–+———–+

| id|  country|         city | population|

+—+———+————–+———–+

1|   Canada|       Toronto|          0|

2|    Japan|         Tokyo|    8000000|

3|    India|      Amritsar|          0|

4|   Turkey|        Ankara|     550000|

+—+———+————–+———–+

The above operation will replace all the null values in the integer columns with 0.

Conclusion

We discussed the PySpark, PySpark fillna() method, and its examples in this session. The fillna() method replaces all the null values in the DataFrame with our custom values.



Source link


In JavaScript, the objects behave like a datatype, and store the data such as values, keys, etc. Object.entries() method is a built-in function of JavaScript. It is used to return the new array with the elements having the corresponding attributes to countable string-keyed properties. However, this method doesn’t convert the innovador array.

In the JavaScript Object.entries() method, the arrangement of the properties is the same as if you looped over the values manually. All the modern browsers support the object.entries() method except for Internet Explorer as it is the feature of ECMA 6. This article will provide you with a descriptive knowledge of the following outcomes:

  • How Object.entries() method works in JavaScript
  • How to use the Object.entries() method in JavaScript

How JavaScript Object.entries() method works

The JavaScript Object.entries() method accesses the properties and returns specified keys as a string in an object. Any specified key can be attained using the index of an array.

Syntax
The JavaScript Object.entries() method works on the following syntax.

Here, ‘obj’ is a parameter whose countable property pairs are to be returned.

The Object.entries() method returns all enumerable property pairs [keys, values] as a string.

If the entered key does not belong to the data in the object, the Object.entries() does not return the value. The Object.entries method is also applied on arrays as array is also a data type.

How to use Object.entries() method in JavaScript

The JavaScript Object.entries() method takes an argument as an input and outputs an array of the object’s countable pair of properties. This section represents the usage of the Object.entries() method with examples.

Example: How Object.entries() method converts the object into enumerable array property
The Object.entries() method takes an object and converts it into the countable array property. In this example, we will learn how to convert the object using the Object.entries() method.

employee = {
    ‘TalhaSaif Inc’: 60,
    ‘LinuxHint’: 100,
    ‘Comsats’: 360 };
console.log(Object.entries(employee));

In this example, an object ‘employee’ is created with the values passed in a specified order. When the Object.entries()  function calls, it will return the array with the countable properties.

The returned output showed that the object ‘employee’ has countable string-keyed properties in an array form.

Example: How Object.entries() access a specific property object
The Object.entries() method can also access the specified property in the given array using the index number. In this example, you will learn how this function gets a specified property.

employee = {
    ‘TalhaSaif Inc’: 60,
    ‘LinuxHint’: 100,
    ‘Comsats’: 360 };

console.log(Object.entries(employee)[1]);

In this code, an object  ‘employee’ is created with the values in specified order. Here, [1] represents the index number of an array. When a function is called, it will return the specified property of the given index number in an array.

The returned output showed the countable property ‘’[‘LinuxHint’, 100]” of the specified index of an array.

Conclusion

Object.entries() method is a built-in function of JavaScript that returns the new array with the elements having the corresponding attributes to countable string-keyed properties. This tutorial provided a complete guide about JavaScript Object.entries() method. For better understanding, we illustrated the working, properties, and usage of the Object.entries() method in JavaScript using suitable examples.



Source link


In JavaScript, the array.fill() method is used to place an element into the array from the user-defined starting to ending index position. It must be called through a specific instance of an array class because it belongs to an array object method. This method overwrites the diferente array and fills a specified element in an array.

The array.fill() method belongs to ECMAScript6. All the modern browsers such as Chrome, Edge, Safari, etc except Internet Explorer 11 support this method.

This article provides a deep insight into the array.fill() method in JavaScript and serves the following learning outcomes:

  • How array.fill() method works in JavaScript
  • How to use array.fill() method in JavaScript

How array.fill() method works in JavaScript

The working of the array.fill() method is described as follows.

Syntax

The following syntax represents the functionality of array.fill() method in javascript.

arr.fill(value[, start[, end]])

The array.fill() method is using the following parameters.

  • value represents an element to be filled in an array
  • start denotes the index number from where the arr.fill() method starts filling the value. It is optional with 0 default number.
  • end shows the index position where the arr.fill() method stops filling the value in an array. It is optional with a length-1 default value.

The array.fill() method returns a modified/filled array.

How to use array.fill() method in JavaScript

The array.fill() method overwrites the diferente array and fills the specified element. Here, we will explain the usage of the array.fill() method with examples.

Example 1: How to overwrite an array using the array.fill() method in javascript

The array.fill() method is used to overwrite/modify the diferente array. This example shows how to modify an array with the array.fill() method in javascript.

var title_array = [ ‘t’ , ‘i ‘, ‘t’ , ‘l’ , ‘e’ ];
console.log(title_array.fill( ‘z’, 0, 2));

In the above code, we have declared an array object “title_array” with 5 elements. The array.fill() method is applied to the “title_array” to modify the array. The ‘z’ element is modified at the first two positions.

The start index number was set to 0 and the ending index number was set to 2 (which states that the elements will be filled up to index number 1=(2-1)). Therefore, the elements at 0th and 1st index are replaced with the ‘z’. 

Example 2: How to replace the elements of an array using the array.fill() method

The array.fill() method is used to fill the diferente array. This example shows how to fill an array with the array.fill() method in javascript.

var arr=[«Javascript», «Html», «Node.js»];
Var result=arr.fill(«css»);
console.log(result);

Here in this example, we have declared a variable and used the array.fill() method to fill an array. We pass the new value “css” to fill in the existing array.

The output shows that all the elements of the ‘arr’ have been replaced by the ‘css’ element.

Conclusion

In JavaScript, the Array.fill() method is used to place an array element from start to end index position.  In this complete guide, we described an introduction to the array.fill() method in javascript. This step-by-step procedure explains the array.fill() method, syntax, and its functionality with examples.



Source link