小编ccs*_*cic的帖子

根据列条件添加包含来自另一个数据帧的值的列

我有两个不同索引长度的数据帧,如下所示:

df_1:

    State  Month  Total Time ... N columns
     AL     4       1000
     AL     5       500
      .
      .
      .
     VA     11      750
     VA     12      1500 
Run Code Online (Sandbox Code Playgroud)

df_2:

    State   Month ... N columns
     AL      4        
     AL      5
      .
      .
      .
     VA      11
     VA      12
Run Code Online (Sandbox Code Playgroud)

如果数据帧之间的“州”和“月”值相同,我想向 df_2 添加一个“总时间”列,该列使用 df_1 的“总时间”列中的值。最终,我会得到:

df_2:

    State  Month  Total Time ... N columns
     AL     4       1000
     AL     5       500
      .
      .
      .
     VA     11      750
     VA     12      1500 
Run Code Online (Sandbox Code Playgroud)

我想有条件地执行此操作,因为索引长度不相同。到目前为止我已经尝试过了:

def f(row):
     if (row['State'] == row['State']) & (row['Month'] == row['Month']): …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

4
推荐指数
1
解决办法
2万
查看次数

您如何将一列系列转换为带有标题的一行系列?

我正在使用Pandas,我想转换这样的系列:

        RT_mean
   0     27
   1     32
   2     10
   3     9
   .
   .
   .   
   190   89
   191   6
Run Code Online (Sandbox Code Playgroud)

到具有标题的一行数据帧,如下所示:

       RT_mean1  RT_mean2  RT_mean3  RT_mean4 ... RT_mean189 RT_mean190
   0      27        32        10        9     ...     89          6
Run Code Online (Sandbox Code Playgroud)

我尝试做,series.transpose()但是在系列上不起作用。任何帮助表示赞赏!

python pandas

1
推荐指数
1
解决办法
45
查看次数

标签 统计

pandas ×2

python ×2

dataframe ×1