I have two data frames, that I am trying to combine.
A json file with headers:
JavaScript
x
5
1
| category 1 | category 2 | category 3 | category 4 |
2
|:-----------|------------:|:------------:|:------------:|
3
| name1 | attribute1 | amount1 | other1 |
4
| name2 | attribute2 | amount2 | other2 |
5
And an Excel file with data in the same format, but without headers:
JavaScript
1
4
1
|:-----------|------------:|:------------:|:------------:|
2
| name3 | attribute3 | amount3 | other3 |
3
| name4 | attribute4 | amount4 | other4 |
4
I am trying to achieve the data frame below:
JavaScript
1
7
1
| category 1 | category 2 | category 3 | category 4 |
2
|:-----------|------------:|:------------:|:------------:|
3
| name1 | attribute1 | amount1 | other1 |
4
| name2 | attribute2 | amount2 | other2 |
5
| name3 | attribute3 | amount3 | other3 |
6
| name4 | attribute4 | amount4 | other4 |
7
My code:
JavaScript
1
8
1
import pandas as pd
2
import json
3
import xlrd
4
5
data = pd.read_json('pandas_test.json', orient='split')
6
data2 = pd.read_excel("guys2.xlsx", header=None)
7
data = pd.concat([data, data2])
8
Problem: When I run my code, the combined data frame looks like this:
JavaScript
1
7
1
| category 1 | category 2 | category 3 | category 4 | 1 | 2 | 3 | 4 |
2
|:-----------|------------:|:------------:|:------------:|:---------:|:----------:|:---------:|:---------:|
3
| name1 | attribute1 | amount1 | other1 | NaN | NaN | NaN | NaN |
4
| name2 | attribute2 | amount2 | other2 | NaN | NaN | NaN | NaN |
5
| NaN | NaN | NaN | NaN | name3 | attribute3 | amount3 | other3 |
6
| NaN | NaN | NaN | NaN | name4 | attribute4 | amount4 | other4 |
7
I have tried the concat function with a few attributes already like ignore_index=True
, but nothing worked so far.
Advertisement
Answer
Just try with
JavaScript
1
3
1
data2.columns=data.columns
2
data = pd.concat([data, data2])
3