“Pandas Produk Cartesian” Kode Jawaban

Produk Pandas Cartesian

a = [1, 2, 3]
b = ["a", "b", "c"]

index = pd.MultiIndex.from_product([a, b], names = ["a", "b"])

pd.DataFrame(index = index).reset_index()
Nasty Narwhal

Pandas Produk Cartesian

# In recent versions of Pandas (>= 1.2) this is built into merge so you can do:
from pandas import DataFrame
df1 = DataFrame({'col1':[1,2],'col2':[3,4]})
df2 = DataFrame({'col3':[5,6]})    

df1.merge(df2, how='cross')
Danila

Mengembalikan produk cartesian dengan bangka lain

# Returns the cartesian product with another DataFrame

df.select("age", "name").collect()
# [Row(age=2, name='Alice'), Row(age=5, name='Bob')]
df2.select("name", "height").collect()
# [Row(name='Tom', height=80), Row(name='Bob', height=85)]
df.crossJoin(df2.select("height")).select(
  "age", "name", "height").collect()
# [Row(age=2, name='Alice', height=80), Row(age=2, name='Alice', height=85), Row(age=55, name='Bob', height=80), Row(age=5, name='Bob', height=85)]
Ethercourt.ml

Jawaban yang mirip dengan “Pandas Produk Cartesian”

Pertanyaan yang mirip dengan “Pandas Produk Cartesian”

Lebih banyak jawaban terkait untuk “Pandas Produk Cartesian” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya