这篇文章的内容最初是作为Pandas Merging 101的一部分,但由于完全公开 这个主题所需的内容的性质和大小,它已被转移到自己的QnA.
给出两个简单的DataFrame;
left = pd.DataFrame({'col1' : ['A', 'B', 'C'], 'col2' : [1, 2, 3]})
right = pd.DataFrame({'col1' : ['X', 'Y', 'Z'], 'col2' : [20, 30, 50]})
left
col1 col2
0 A 1
1 B 2
2 C 3
right
col1 col2
0 X 20
1 Y 30
2 Z 50
Run Code Online (Sandbox Code Playgroud)
可以计算这些帧的叉积,看起来像:
A 1 X 20
A 1 Y 30
A 1 Z 50
B 2 X 20
B 2 Y 30
B 2 Z 50 …
Run Code Online (Sandbox Code Playgroud) 我有数据框,每一行都有一个列表值。
id list_of_value
0 ['a','b','c']
1 ['d','b','c']
2 ['a','b','c']
3 ['a','b','c']
Run Code Online (Sandbox Code Playgroud)
我必须用一行和所有其他行计算分数
例如:
Step 1: Take value of id 0: ['a','b','c'],
Step 2: find the intersection between id 0 and id 1 ,
resultant = ['b','c']
Step 3: Score Calculation => resultant.size / id.size
Run Code Online (Sandbox Code Playgroud)
在 id 0 和 id 1,2,3 之间重复步骤 2,3,对于所有 id 都类似。
并创建一个 N x N 数据框;像这样:
- 0 1 2 3
0 1 0.6 1 1
1 1 1 1 1
2 1 1 1 1
3 …
Run Code Online (Sandbox Code Playgroud) 我找不到任何关于交叉连接的内容,包括合并/加入或其他一些.我需要使用{my function}作为myfunc来处理两个数据帧.相当于:
{
for itemA in df1.iterrows():
for itemB in df2.iterrows():
t["A"] = myfunc(itemA[1]["A"],itemB[1]["A"])
}
Run Code Online (Sandbox Code Playgroud)
相当于:
{
select myfunc(df1.A,df2.A),df1.A,df2.A from df1,df2;
}
Run Code Online (Sandbox Code Playgroud)
但我需要更有效的解决方案:如果使用apply我将是如何实现它们thx; ^^
假设我有两张桌子:
表格1:
col1 col2
0 1
2 3
Run Code Online (Sandbox Code Playgroud)
表2:
col3 col4
5 6
7 8
Run Code Online (Sandbox Code Playgroud)
在SQL中,如果我发出以下声明:
Select *
From Table1, Table2;
Run Code Online (Sandbox Code Playgroud)
我期望从两个表中获得包含所有组合的表格:
col1 col2 col3 col4
0 1 5 6
0 1 7 8
2 3 5 6
2 3 7 8
Run Code Online (Sandbox Code Playgroud)
有没有办法在pandas中使用两个数据帧做同样的事情?
我一直在尝试为数据框的每一行分配一个值,但我一直没有这样做(我是熊猫新手),所以如果有人可以提供帮助,我将非常感激!
我有两个数据框。在输入数据框中,我有以下品牌:
brand_raw.head()
brand_name
0 Nike
1 Lacoste
2 Adidas
Run Code Online (Sandbox Code Playgroud)
然后,在输出数据集上,我有一些对象:
object_raw.head()
category_id object_name
0 24 T-shirt
1 45 Shorts
2 32 Dress
Run Code Online (Sandbox Code Playgroud)
我需要的是一个包含所有对象和所有品牌的数据框:
to_raw.head()
category_id object_name brand_name
0 24 T-shirt Nike
1 45 Shorts Nike
2 32 Dress Nike
3 24 T-shirt Lacoste
4 45 Shorts Lacoste
5 32 Dress Lacoste
6 24 T-shirt Adidas
7 45 Shorts Adidas
8 32 Dress Adidas
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用apply函数,对行进行迭代,但是最终覆盖了值,所以我写了最后一个品牌:
0 24 T-shirt Nike
1 45 Shorts Nike
2 32 Dress Nike
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
def insert_value_in_every_row(input_df, …
Run Code Online (Sandbox Code Playgroud) 我有2个数据帧,df1
并df2
希望执行以下操作,将结果存储在df3
:
for each row in df1:
for each row in df2:
create a new row in df3 (called "df1-1, df2-1" or whatever) to store results
for each cell(column) in df1:
for the cell in df2 whose column name is the same as for the cell in df1:
compare the cells (using some comparing function func(a,b) ) and,
depending on the result of the comparison, write result into the
appropriate column of the "df1-1, df2-1" …
Run Code Online (Sandbox Code Playgroud) 假设我们有一个DataFrame df
df = pd.DataFrame({
"Id": [1, 2],
"Value": [2, 5]
})
df
Id Value
0 1 2
1 2 5
Run Code Online (Sandbox Code Playgroud)
还有一些函数f
需要一个元素df
并返回一个DataFrame。
def f(value):
return pd.DataFrame({"A": range(10, 10 + value), "B": range(20, 20 + value)})
f(2)
A B
0 10 20
1 11 21
Run Code Online (Sandbox Code Playgroud)
我们希望将应用于f
中的每个元素df["Value"]
,并将结果连接到中df
,如下所示:
Id Value A B
0 1 2 10 20
1 1 2 11 21
2 2 5 10 20
2 2 5 11 21
2 …
Run Code Online (Sandbox Code Playgroud) 给定任意数量的列表,我想生成一个熊猫DataFrame
作为笛卡尔积。例如,给定:
a = [1, 2, 3]
b = ['val1', 'val2']
c = [100, 101]
Run Code Online (Sandbox Code Playgroud)
我想DataFrame
以列a
、b
、 和c
以及所有 3x2x2=12 组合结束。
与pandas 中的笛卡尔积不同,我正在寻找提供两个以上输入的能力,并且我不希望传递DataFrame
s,这将涉及将值保持在相同的范围内DataFrame
而不是将其组合。这个问题的答案可能不会与那个问题的答案重叠。
与x 和 y 数组点的笛卡尔积点成 2D 点的单个数组不同,我正在寻找DataFrame
带有命名列的 Pandas结果,而不是二维 numpy 数组。
我试图通过交叉连接两个现有的csv文件来创建一个新的csv文件.
csv文件#1:
hour Elevation Azimuth x y z sunx suny sunz
06:29:00 -0.833 67.72 0.379094033 0.925243946 -0.014538068 0.379094033 0.925243946 -0.014538068
07:00:00 6.28 68.75 0.360264063 0.92641472 0.109387255 0.360264063 0.92641472 0.109387255
Run Code Online (Sandbox Code Playgroud)
csv文件#2:
ID SURFACES A1X A1Y A1Z A2X A2Y A2Z B1X B1Y B1Z B2X B2Y B2Z AX AY AZ BX BY BZ ABX ABY ABZ planex planey planez
1 GROUND 800085.3323 961271.977 -3.07E-18 800080.8795 961246.1978 -3.07E-18 800097.1572 961269.9344 -3.07E-18 800085.3323 961271.977 -3.07E-18 4.4528 25.7792 0.00E+00 11.8249 -2.0426 0.00E+00 0 0 -313.9317514 0 …
Run Code Online (Sandbox Code Playgroud) 我有一些 python 代码,它运行一个简单的 for 循环并打印出结果的每个组合,我试图根据结果产生的顺序弄清楚如何将这些全部附加到单个数据帧中。我将在下面解释.
我有以下代码:
categories = ['small', 'medium', 'big']
parameters = ['p1_5_p2_4_p3_2', 'p1_3_p2_8_p3_3', 'p1_4_p2_3_p3_6']
Blue = [5, 4, 3]
for parameter in parameters:
for category in categories:
for x in Blue:
y = x + 1
z = x + 2
print(category)
print(parameter)
print(y)
print(z)
print('')
Run Code Online (Sandbox Code Playgroud)
它产生:
small
p1_5_p2_4_p3_2
6
7
small
p1_5_p2_4_p3_2
5
6
small
p1_5_p2_4_p3_2
4
5
medium
p1_5_p2_4_p3_2
6
7
medium
p1_5_p2_4_p3_2
5
6
medium
p1_5_p2_4_p3_2
4
5
big
p1_5_p2_4_p3_2
6
7
big
p1_5_p2_4_p3_2
5 …
Run Code Online (Sandbox Code Playgroud)