pandas TypeError:索引不支持可变操作 [英] Pandas TypeError: Index does not support mutable operations

查看:70
本文介绍了 pandas TypeError:索引不支持可变操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设目录中存在三个.csv文件:EX1.csv,EX2.csv,EX3.csv.

Assume there are three .csv files in the directory: EX1.csv, EX2.csv, EX3.csv.

我已经使用以下代码将它们合并在一起.

I have used the following code to merge them together.

import pandas as pd

EX1 = pd.read_csv('EX1.csv')
EX2 = pd.read_csv('EX2.csv')
EX3 = pd.read_csv('EX3.csv')

data = [EX1, EX2, EX3]
data = pd.concat(data)
data = pd.DataFrame(data)

一旦使用串联,索引号就不会跟随渐进的行编号.

Once concatenation is used, the index numbers do not follow a gradual row numbering.

我的目标之一是确保合并数据帧的索引号从0到(len(data)-1).为了使索引号从0逐渐增加到(len(data)-1),我使用了下面的代码并得到了TypeError.

One of my goals is to make sure that the index numbers for the merged data-frame goes from 0 to (len(data) - 1). To make the index numbers follow a gradual increase from 0 to (len(data) - 1), I used the code below and got the TypeError.

for i in range(len(data)):
    data.index[i] = i

请您对此提出一些建议.谢谢!

I would appreciate some advice on this please. Thanks!

推荐答案

concat ignore_index

df1=pd.DataFrame([1,'a'])
df2=pd.DataFrame([2,'b'])
pd.concat([df1,df2],ignore_index=True)
Out[873]: 
   0
0  1
1  a
2  2
3  b

这篇关于 pandas TypeError:索引不支持可变操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆