@@ -109,6 +109,10 @@ import Synchronization
109109/// - ``dropDuplicatesWithinWatermark(_:)``
110110/// - ``distinct()``
111111/// - ``withColumnRenamed(_:_:)``
112+ /// - ``unpivot(_:_:_:)``
113+ /// - ``unpivot(_:_:_:_:)``
114+ /// - ``melt(_:_:_:)``
115+ /// - ``melt(_:_:_:_:)``
112116///
113117/// ### Join Operations
114118/// - ``join(_:)``
@@ -1202,6 +1206,106 @@ public actor DataFrame: Sendable {
12021206 return dropDuplicates ( )
12031207 }
12041208
1209+ /// Transposes a DataFrame, switching rows to columns. This function transforms the DataFrame
1210+ /// such that the values in the first column become the new columns of the DataFrame.
1211+ /// - Returns: A transposed ``DataFrame``.
1212+ public func transpose( ) -> DataFrame {
1213+ return buildTranspose ( [ ] )
1214+ }
1215+
1216+ /// Unpivot a DataFrame from wide format to long format, optionally leaving identifier columns
1217+ /// set. This is the reverse to `groupBy(...).pivot(...).agg(...)`, except for the aggregation,
1218+ /// which cannot be reversed. This is an alias for `unpivot`.
1219+ /// - Parameters:
1220+ /// - ids: ID column names
1221+ /// - values: Value column names to unpivot
1222+ /// - variableColumnName: Name of the variable column
1223+ /// - valueColumnName: Name of the value column
1224+ /// - Returns: A ``DataFrame``.
1225+ public func melt(
1226+ _ ids: [ String ] ,
1227+ _ values: [ String ] ,
1228+ _ variableColumnName: String ,
1229+ _ valueColumnName: String
1230+ ) -> DataFrame {
1231+ return unpivot ( ids, values, variableColumnName, valueColumnName)
1232+ }
1233+
1234+ /// Unpivot a DataFrame from wide format to long format, optionally leaving identifier columns
1235+ /// set. This is the reverse to `groupBy(...).pivot(...).agg(...)`, except for the aggregation,
1236+ /// which cannot be reversed. This is an alias for `unpivot`.
1237+ /// - Parameters:
1238+ /// - ids: ID column names
1239+ /// - variableColumnName: Name of the variable column
1240+ /// - valueColumnName: Name of the value column
1241+ /// - Returns: A ``DataFrame``.
1242+ public func melt(
1243+ _ ids: [ String ] ,
1244+ _ variableColumnName: String ,
1245+ _ valueColumnName: String
1246+ ) -> DataFrame {
1247+ return unpivot ( ids, variableColumnName, valueColumnName)
1248+ }
1249+
1250+ /// Unpivot a DataFrame from wide format to long format, optionally leaving identifier columns
1251+ /// set. This is the reverse to `groupBy(...).pivot(...).agg(...)`, except for the aggregation,
1252+ /// which cannot be reversed.
1253+ /// - Parameters:
1254+ /// - ids: ID column names
1255+ /// - values: Value column names to unpivot
1256+ /// - variableColumnName: Name of the variable column
1257+ /// - valueColumnName: Name of the value column
1258+ /// - Returns: A ``DataFrame``.
1259+ public func unpivot(
1260+ _ ids: [ String ] ,
1261+ _ values: [ String ] ,
1262+ _ variableColumnName: String ,
1263+ _ valueColumnName: String
1264+ ) -> DataFrame {
1265+ return buildUnpivot ( ids, values, variableColumnName, valueColumnName)
1266+ }
1267+
1268+ /// Unpivot a DataFrame from wide format to long format, optionally leaving identifier columns
1269+ /// set. This is the reverse to `groupBy(...).pivot(...).agg(...)`, except for the aggregation,
1270+ /// which cannot be reversed.
1271+ /// - Parameters:
1272+ /// - ids: ID column names
1273+ /// - variableColumnName: Name of the variable column
1274+ /// - valueColumnName: Name of the value column
1275+ /// - Returns: A ``DataFrame``.
1276+ public func unpivot(
1277+ _ ids: [ String ] ,
1278+ _ variableColumnName: String ,
1279+ _ valueColumnName: String
1280+ ) -> DataFrame {
1281+ return buildUnpivot ( ids, nil , variableColumnName, valueColumnName)
1282+ }
1283+
1284+ func buildUnpivot(
1285+ _ ids: [ String ] ,
1286+ _ values: [ String ] ? ,
1287+ _ variableColumnName: String ,
1288+ _ valueColumnName: String ,
1289+ ) -> DataFrame {
1290+ let plan = SparkConnectClient . getUnpivot ( self . plan. root, ids, values, variableColumnName, valueColumnName)
1291+ return DataFrame ( spark: self . spark, plan: plan)
1292+ }
1293+
1294+ /// Transposes a ``DataFrame`` such that the values in the specified index column become the new
1295+ /// columns of the ``DataFrame``.
1296+ /// - Parameter indexColumn: The single column that will be treated as the index for the transpose operation.
1297+ /// This column will be used to pivot the data, transforming the DataFrame such that the values of
1298+ /// the indexColumn become the new columns in the transposed DataFrame.
1299+ /// - Returns: A transposed ``DataFrame``.
1300+ public func transpose( _ indexColumn: String ) -> DataFrame {
1301+ return buildTranspose ( [ indexColumn] )
1302+ }
1303+
1304+ func buildTranspose( _ indexColumn: [ String ] ) -> DataFrame {
1305+ let plan = SparkConnectClient . getTranspose ( self . plan. root, indexColumn)
1306+ return DataFrame ( spark: self . spark, plan: plan)
1307+ }
1308+
12051309 /// Groups the DataFrame using the specified columns.
12061310 ///
12071311 /// This method is used to perform aggregations on groups of data.
0 commit comments