Skip to content

Commit 01433ca

Browse files
Add more DataFrame operations examples (#599)
1 parent f803aa8 commit 01433ca

File tree

2 files changed

+38
-0
lines changed
  • examples
    • Microsoft.Spark.CSharp.Examples/Sql/Batch
    • Microsoft.Spark.FSharp.Examples/Sql

2 files changed

+38
-0
lines changed

examples/Microsoft.Spark.CSharp.Examples/Sql/Batch/Basic.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ public void Run(string[] args)
108108

109109
DataFrame joinedDf3 = df.Join(df, df["name"] == df["name"], "outer");
110110
joinedDf3.Show();
111+
112+
// Union of two data frames
113+
DataFrame unionDf = df.Union(df);
114+
unionDf.Show();
115+
116+
// Add new column to data frame
117+
df.WithColumn("location", Lit("Seattle")).Show();
118+
119+
// Rename existing column
120+
df.WithColumnRenamed("name", "fullname").Show();
121+
122+
// Filter rows with null age
123+
df.Filter(Col("age").IsNull()).Show();
124+
125+
// Fill null values in age column with -1
126+
df.Na().Fill(-1, new[] { "age" }).Show();
127+
128+
// Drop age column
129+
df.Drop(new[] { "age" }).Show();
111130

112131
spark.Stop();
113132
}

examples/Microsoft.Spark.FSharp.Examples/Sql/Basic.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ type Basic() =
7878

7979
let joinedDf3 = df.Join(df, df.["name"].EqualTo(df.["name"]), "outer")
8080
joinedDf3.Show()
81+
82+
// Union of two data frames
83+
let unionDf = df.Union(df)
84+
unionDf.Show()
85+
86+
// Add new column to data frame
87+
df.WithColumn("location", Functions.Lit("Seattle")).Show()
88+
89+
// Rename existing column
90+
df.WithColumnRenamed("name", "fullname").Show()
91+
92+
// Filter rows with null age
93+
df.Filter(df.["age"].IsNull()).Show()
94+
95+
// Fill null values in age column with -1
96+
df.Na().Fill(-1L, ["age"]).Show()
97+
98+
// Drop age column
99+
df.Drop(df.["age"]).Show()
81100

82101
spark.Stop()
83102
0

0 commit comments

Comments
 (0)