联合两个数据框

时间:2018-06-10 19:13:50

标签: python pandas d3.js charts c3.js

我想将这两个数据帧(没有公共列)合并在一起。这两个数据框看起来像这样:

DF1:

10.74,5.71,5.41
11.44,6.1,5.87

DF2:

10.17,6.58,5.23
9.99,5.75,5.13
11.21,6.35,5.72
10.3,5.86,5.12

我正在尝试:

df_total=pd.concat([df1,df2],axis=1)

但结果看起来像这样:

Access grade global,Grade_global,Regression_global,Access grade,Grade,Regression
,,,10.74,5.71,5.41
,,,11.44,6.1,5.87
10.17,6.58,5.23,,,
9.99,5.75,5.13,,,
11.21,6.35,5.72,,,
10.3,5.86,5.12,,,

我有什么这样的东西:

10.17,6.58,5.23,10.74,5.71,5.41
9.99,5.75,5.13,11.44,6.1,5.87
11.21,6.35,5.72
10.3,5.86,5.12

我想知道如何做的两件事是:

1-如何合并2个数据帧,使值彼此相邻(因此,行数应该是两个数据帧之间的最大行数;在这种情况下为4)。 2-如何避免使用NaN(你可以看到最后有多个逗号)。 (我想避免这种情况,因为之后在我使用的散点图中,所有的Nan都被绘制为0(所以我在y = 0时有一行点)。

Nan值正在生成零。请看结果:

Result with zero values in the axis

html剪辑是:

<div style="line-height:77%;"><br></div>
    <div id="grade_access_hs"></div>
    <div style="line-height:77%;"><br></div>
    <p>The lines that best approximate the expected grades according to the access grade to University and comparing all students with {{user.hsname}}' students are:</p>
    <div style="line-height:30%;"><br></div>
    <div id="equation3"></div>
    <div style="line-height:30%;"><br></div>
    <div id="equation4"></div>
    <script type="text/javascript" src="../static/scripts/grade_access_hs.js"></script>

所有图表:

  <script>
    'use strict';
var Grade_access_hs = c3.generate({
  bindto: '#grade_access_hs',
  data: {
    url: '../static/CSV/Chart_data/grades_access_hs.csv',
    xs: {
        Grade_global: 'Access grade global',
        Grade: 'Access grade',
        Regression_global: 'Access grade global',
        Regression: 'Access grade'
    },
    types: {
        Grade_global:'scatter',
        Grade:'scatter',
        Regression_global: 'line',
        Regression: 'line'
    },
  },
  axis: {
    y: {
      label: {
        text: "Average grade",
        position: "outer-middle"
      },
      min: 1,
      max: 9,
      tick: {outer: false}
    },
    x: {
      label: {
        text: "Access grade PAU",
        position: "outer-center"
      },
      min: 9,
      max: 14,
      tick: {
        outer: false,
        count:1,
        fit:false,
        values: [9,10,11,12,13,14]
      } 
    }
  },
  size: {
    height: 400,
    width: 800
  },
  zoom: {
    enabled: true
  },
  legend: {
    show: true,
    position: 'inset',
    inset: {
      anchor: 'top-right',
      x: 20,
      y: 20
    }
  },
})

d3.csv('../static/CSV/Chart_data/grades_access_hs.csv',function(data){
  var d1 = data[0];
  var d2 = data[1];

  var b = (1-(d2['Regression_global']/d1['Regression_global']))/((d1['Access grade global']-d2['Access grade global'])/d1['Regression_global'])
  var a = d1['Regression_global'] - (b * d1['Access grade global'])
  b = (Math.round(b*1000)/1000);
  a = (Math.round(a*1000)/1000);
  document.getElementById("equation3").innerHTML = "Global: Grade = " + a + "·x + " + b;

  var d = (1-(d2['Regression']/d1['Regression']))/((d1['Access grade private']-d2['Access grade private'])/d1['Regression'])
  var c = d1['Regression'] - (b * d1['Access grade'])
  d = (Math.round(d*1000)/1000);
  c = (Math.round(c*1000)/1000);
  document.getElementById("equation4").innerHTML = "Specific high school: Grade = " + c + "·x + " + d;
})
  </script>

使用grades_acess_hs.csv:

Access grade global,Grade_global,Regression_global,Access grade,Grade,Regression
,,,10.74,5.71,5.41
,,,11.44,6.1,5.87
,,,11.21,6.35,5.72
,,,10.3,5.86,5.12
10.17,6.58,5.23,,,
9.99,5.75,5.13,,,
10.96,5.84,5.71,,,
9.93,6.12,5.09,,,
9.93,6.0,5.09,,,
11.21,6.22,5.86,,,
11.28,6.1,5.9,,,
,,,10.93,6.08,5.54

提前致谢!

2 个答案:

答案 0 :(得分:1)

我认为您需要joinredux-thunk

//the action returns a function, which gets called with store.dispatch as an argument
const myAction = someArg => dispatch => {
    createSocket(url, dispatch);
};

答案 1 :(得分:0)

没有太多考虑:

@RestController
public class UserDetailsController {


    private final UserRepository userRepository;

    @Autowired
    public UserDetailsController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping("/user-details")
    public AppUser getUserDetails(Principal principal) {   
        return userRepository.findByUserName(principal.getName());
    }

}