Skip to content
219 changes: 183 additions & 36 deletions python/pyspark/sql/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,17 @@ def schema(self) -> StructType:

Examples
Comment thread
HyukjinKwon marked this conversation as resolved.
--------
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+
>>> df.schema
Comment thread
Transurgeon marked this conversation as resolved.
StructType([StructField('age', IntegerType(), True),
StructField('name', StringType(), True)])
StructType([StructField('age', LongType(), True), StructField('name', StringType(), True)])
Comment thread
Transurgeon marked this conversation as resolved.
Outdated
"""
if self._schema is None:
try:
Expand Down Expand Up @@ -571,29 +579,41 @@ def show(self, n: int = 20, truncate: Union[bool, int] = True, vertical: bool =

Examples
--------
>>> df
DataFrame[age: int, name: string]
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 2|Alice|
| 5| Bob|
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+
>>> df.show(2)
Comment thread
Transurgeon marked this conversation as resolved.
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
+---+-----+
only showing top 2 rows
>>> df.show(truncate=3)
+---+----+
|age|name|
+---+----+
| 2| Ali|
| 5| Bob|
| 14| Tom|
| 23| Ali|
| 16| Bob|
+---+----+
>>> df.show(vertical=True)
-RECORD 0-----
age | 2
name | Alice
age | 14
name | Tom
-RECORD 1-----
age | 5
name | Bob
age | 23
name | Alice
-RECORD 2-----
age | 16
name | Bob
"""

if not isinstance(n, int) or isinstance(n, bool):
Expand Down Expand Up @@ -798,8 +818,18 @@ def count(self) -> int:

Examples
--------
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+

>>> df.count()
Comment thread
Transurgeon marked this conversation as resolved.
2
3
"""
return int(self._jdf.count())

Expand Down Expand Up @@ -862,8 +892,17 @@ def take(self, num: int) -> List[Row]:

Examples
--------
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+
>>> df.take(2)
Comment thread
Transurgeon marked this conversation as resolved.
[Row(age=2, name='Alice'), Row(age=5, name='Bob')]
[Row(age=14, name='Tom'), Row(age=23, name='Alice')]
"""
return self.limit(num).collect()

Expand All @@ -878,8 +917,17 @@ def tail(self, num: int) -> List[Row]:

Examples
--------
>>> df.tail(1)
[Row(age=5, name='Bob')]
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+
>>> df.tail(2)
[Row(age=23, name='Alice'), Row(age=16, name='Bob')]
"""
with SCCallSiteSync(self._sc):
sock_info = self._jdf.tailToPython(num)
Expand Down Expand Up @@ -1179,6 +1227,16 @@ def distinct(self) -> "DataFrame":

Examples
--------
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (23, "Alice")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 23|Alice|
+---+-----+

>>> df.distinct().count()
Comment thread
Transurgeon marked this conversation as resolved.
2
"""
Expand Down Expand Up @@ -1375,8 +1433,17 @@ def dtypes(self) -> List[Tuple[str, str]]:

Examples
--------
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
+---+-----+

>>> df.dtypes
Comment thread
Transurgeon marked this conversation as resolved.
[('age', 'int'), ('name', 'string')]
[('age', 'bigint'), ('name', 'string')]
"""
return [(str(f.name), f.dataType.simpleString()) for f in self.schema.fields]

Expand Down Expand Up @@ -2743,7 +2810,17 @@ def fillna(

Examples
--------
>>> df4.na.fill(50).show()
>>> df = spark.createDataFrame([(10, 80, "Alice"), (5, None, "Bob"), (None, None, "Tom"), (None, None, None)], ["age", "height", "name"])
>>> df.show()
+----+------+-----+
| age|height| name|
+----+------+-----+
| 10| 80|Alice|
| 5| null| Bob|
|null| null| Tom|
|null| null| null|
+----+------+-----+
>>> df.na.fill(50).show()
Comment thread
Transurgeon marked this conversation as resolved.
+---+------+-----+
|age|height| name|
+---+------+-----+
Expand All @@ -2753,7 +2830,16 @@ def fillna(
| 50| 50| null|
+---+------+-----+

>>> df5.na.fill(False).show()
>>> df = spark.createDataFrame([(10, "Alice", None), (5, "Bob", None), (None, "Mallory", True)], ["age", "name", "spy"])
>>> df.show()
+----+-------+----+
| age| name| spy|
+----+-------+----+
| 10| Alice|null|
| 5| Bob|null|
|null|Mallory|true|
+----+-------+----+
>>> df.na.fill(False).show()
Comment thread
Transurgeon marked this conversation as resolved.
+----+-------+-----+
| age| name| spy|
+----+-------+-----+
Expand All @@ -2762,7 +2848,17 @@ def fillna(
|null|Mallory| true|
+----+-------+-----+

>>> df4.na.fill({'age': 50, 'name': 'unknown'}).show()
>>> df = spark.createDataFrame([(10, 80, "Alice"), (5, None, "Bob"), (None, None, "Tom"), (None, None, None)], ["age", "height", "name"])
>>> df.show()
+----+------+-----+
| age|height| name|
+----+------+-----+
| 10| 80|Alice|
| 5| null| Bob|
|null| null| Tom|
|null| null| null|
+----+------+-----+
>>> df.na.fill({'age': 50, 'name': 'unknown'}).show()
+---+------+-------+
|age|height| name|
+---+------+-------+
Expand Down Expand Up @@ -2869,7 +2965,17 @@ def replace( # type: ignore[misc]

Examples
--------
>>> df4.na.replace(10, 20).show()
>>> df = spark.createDataFrame([(10, 80, "Alice"), (5, None, "Bob"), (None, None, "Tom"), (None, None, None)], ["age", "height", "name"])
>>> df.show()
+----+------+-----+
| age|height| name|
+----+------+-----+
| 10| 80|Alice|
| 5| null| Bob|
|null| null| Tom|
|null| null| null|
+----+------+-----+
>>> df.na.replace(10, 20).show()
Comment thread
Transurgeon marked this conversation as resolved.
+----+------+-----+
| age|height| name|
+----+------+-----+
Expand All @@ -2879,17 +2985,19 @@ def replace( # type: ignore[misc]
|null| null| null|
+----+------+-----+

>>> df4.na.replace('Alice', None).show()
+----+------+----+
| age|height|name|
+----+------+----+
| 10| 80|null|
| 5| null| Bob|
|null| null| Tom|
|null| null|null|
+----+------+----+
Replace all instances of Alice to null

>>> df4.na.replace({'Alice': None}).show()
>>> df = spark.createDataFrame([(10, 80, "Alice"), (5, None, "Bob"), (None, None, "Tom"), (None, None, None)], ["age", "height", "name"])
>>> df.show()
+----+------+-----+
| age|height| name|
+----+------+-----+
| 10| 80|Alice|
| 5| null| Bob|
|null| null| Tom|
|null| null| null|
+----+------+-----+
>>> df.na.replace('Alice', None).show()
+----+------+----+
| age|height|name|
+----+------+----+
Expand All @@ -2899,7 +3007,19 @@ def replace( # type: ignore[misc]
|null| null|null|
+----+------+----+

>>> df4.na.replace(['Alice', 'Bob'], ['A', 'B'], 'name').show()
Replace all instances of Alice to 'A' and Bob to 'B' under the name column

>>> df = spark.createDataFrame([(10, 80, "Alice"), (5, None, "Bob"), (None, None, "Tom"), (None, None, None)], ["age", "height", "name"])
>>> df.show()
+----+------+-----+
| age|height| name|
+----+------+-----+
| 10| 80|Alice|
| 5| null| Bob|
|null| null| Tom|
|null| null| null|
+----+------+-----+
>>> df.na.replace(['Alice', 'Bob'], ['A', 'B'], 'name').show()
+----+------+----+
| age|height|name|
+----+------+----+
Expand Down Expand Up @@ -3356,11 +3476,29 @@ def drop(self, *cols: "ColumnOrName") -> "DataFrame": # type: ignore[misc]

Examples
--------
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+
>>> df.drop('age').collect()
Comment thread
Transurgeon marked this conversation as resolved.
Outdated
[Row(name='Alice'), Row(name='Bob')]
[Row(name='Tom'), Row(name='Alice'), Row(name='Bob')]

>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+
>>> df.drop(df.age).collect()
[Row(name='Alice'), Row(name='Bob')]
[Row(name='Tom'), Row(name='Alice'), Row(name='Bob')]

>>> df.join(df2, df.name == df2.name, 'inner').drop(df.name).collect()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what these 3 inner joins do exactly. I dont see anywhere an instantiation of df2..

What should I do with these 3 examples?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's showing a common example that join and drop the join key.

[Row(age=5, height=85, name='Bob')]
Expand Down Expand Up @@ -3393,12 +3531,21 @@ def toDF(self, *cols: "ColumnOrName") -> "DataFrame":
Parameters
----------
cols : str
new column names
new column names. The length of the list needs to be the same as the number of columns in the initial DataFrame
Comment thread
Transurgeon marked this conversation as resolved.
Outdated

Examples
--------
>>> df = spark.createDataFrame([(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
>>> df.show()
+---+-----+
|age| name|
+---+-----+
| 14| Tom|
| 23|Alice|
| 16| Bob|
+---+-----+
>>> df.toDF('f1', 'f2').collect()
[Row(f1=2, f2='Alice'), Row(f1=5, f2='Bob')]
[Row(f1=14, f2='Tom'), Row(f1=23, f2='Alice'), Row(f1=16, f2='Bob')]
"""
jdf = self._jdf.toDF(self._jseq(cols))
return DataFrame(jdf, self.sparkSession)
Expand Down