Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pandas.concat两个数据帧(一个带标题,一个不带标题)_Python_Python 3.x_Pandas_Xlrd - Fatal编程技术网

Python pandas.concat两个数据帧(一个带标题,一个不带标题)

Python pandas.concat两个数据帧(一个带标题,一个不带标题),python,python-3.x,pandas,xlrd,Python,Python 3.x,Pandas,Xlrd,我有两个数据帧,我正在尝试合并 带有标题的json文件: | category 1 | category 2 | category 3 | category 4 | |:-----------|------------:|:------------:|:------------:| | name1 | attribute1 | amount1 | other1 | | name2 | attribute2 | amount2 |

我有两个数据帧,我正在尝试合并

带有标题的json文件:

| category 1 | category 2  | category 3   | category 4   |
|:-----------|------------:|:------------:|:------------:|
|   name1    | attribute1  |   amount1    | other1       |
|   name2    | attribute2  |   amount2    | other2       |
以及包含相同格式数据但没有标题的Excel文件:

|:-----------|------------:|:------------:|:------------:|
|   name3    | attribute3  |   amount3    | other3       |
|   name4    | attribute4  |   amount4    | other4       |
我试图实现以下数据框架:

| category 1 | category 2  | category 3   | category 4   |
|:-----------|------------:|:------------:|:------------:|
|   name1    | attribute1  |   amount1    | other1       |
|   name2    | attribute2  |   amount2    | other2       |
|   name3    | attribute3  |   amount3    | other3       |
|   name4    | attribute4  |   amount4    | other4       |
我的代码:

import pandas as pd
import json
import xlrd

data = pd.read_json('pandas_test.json', orient='split')
data2 = pd.read_excel("guys2.xlsx", header=None)
data = pd.concat([data, data2])
问题: 当我运行代码时,组合数据框如下所示:

| category 1 | category 2  | category 3   | category 4   |     1     |     2      |     3     |     4     |
|:-----------|------------:|:------------:|:------------:|:---------:|:----------:|:---------:|:---------:|
|   name1    | attribute1  |   amount1    | other1       |   NaN     |    NaN     |   NaN     |   NaN     |
|   name2    | attribute2  |   amount2    | other2       |   NaN     |    NaN     |   NaN     |   NaN     |
|    NaN     |     NaN     |     NaN      |    NaN       |  name3    | attribute3 |   amount3 |   other3  |
|    NaN     |     NaN     |     NaN      |    NaN       |  name4    | attribute4 |   amount4 |   other4  |
我已经用一些属性尝试了concat函数,比如
ignore\u index=True
,但到目前为止没有任何效果。

只需尝试一下

data2.columns=data.columns
data = pd.concat([data, data2])

合并这些值并创建新的数据帧

import numpy as np
pd.DataFrame(np.concatenate((df1.values,df2.values)),columns=df1.columns)

对于concatenate,我可以考虑的一个解决方案是定义列名称并使用列表1列和列表2

试试下面的

data = pd.concat([data, data2])columns=data.columns)
范例

np.random.seed(100)
df1 = pd.DataFrame(np.random.randint(10, size=(2,3)), columns=list('ABF'))
print (df1)
df2 = pd.DataFrame(np.random.randint(10, size=(1,3)), columns=list('ERT'))
print (df2)
输出

A  B  F

0  8  8  3

1  7  7  0

E  R  T
0  4  2  5
使用Df1列表中的列

df = pd.DataFrame(np.concatenate([df1.values, df2.values]), columns=df1.columns)
print (df)

 A  B  F
0  8  8  3
1  7  7  0
2  4  2  5

@VBAPete yw:-)节日快乐~当我尝试删除第一行数据2时。