pandas将索引类型从RangeIndex转换为Int64Index

2024-10-02 12:29:10 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将RangeIndex类型转换为Int64Index类型?我有两个数据帧,都是以相同的方式从.csv文件导入的。Pandas会自动将一个设置为int64索引,另一个设置为RangeIndex。当我为两个数据帧准备好以下代码(根据另外两列中的值创建一个新列)时,我得到了一个错误。我想使两个数据帧的类型相同,这样我的代码就可以同时为两个数据帧生成新的列,稍后我将使用这些列进行合并。在

这段代码适用于Int64Index,但不适用于Range,并且我确认了两个dataframe中的相关字段(列)是相同的。在

这对于Int64Index数据帧(df_new)非常有用:

# create new column by combining data in 3 other columns
df_new['ExpWLTh']=df_new['ExpNum'].astype(str)+'-'+df_new['WL'].astype(str)+'-'+df_new['Threshold'].astype(str)

相同的代码在RangeIndex dataframe(df_val)中不起作用,即使相关列的数据类型相同:

^{pr2}$

当我尝试创建新列时,RangeIndex dataframe(df_val)会出现以下错误:

unorderable types: str() < int()

以下是每个df中数据类型的详细信息:

df_val:
None
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 218 entries, 0 to 217
Data columns (total 15 columns):
Person               218 non-null object
Threshold            218 non-null int64
WL                   218 non-null int64
Threshold            218 non-null float64
Energy sum           218 non-null float64
White sum            218 non-null float64
Diff (energy)        218 non-null float64
Scaled energy        218 non-null float64
Sens (energy)        218 non-null float64
Sens (quanta)        218 non-null float64
Log sens (quanta)    218 non-null float64
Add 3                218 non-null float64
BkgdLt               218 non-null int64
BkgdLt_b             218 non-null object
ExpNum               218 non-null object
dtypes: float64(9), int64(3), object(3)
memory usage: 25.6+ KB
None
df_new:
None
<class 'pandas.core.frame.DataFrame'>
Int64Index: 7043 entries, 0 to 7839
Data columns (total 15 columns):
File             7043 non-null object
Threshold        7043 non-null int64
StepSize         7043 non-null object
RevNum           7028 non-null float64
WL               7043 non-null int64
RevPos           7028 non-null float64
BkgdLt           7043 non-null int32
Date             7043 non-null datetime64[ns]
Person           7043 non-null object
AbRevPos         7028 non-null float64
ExpNum           7043 non-null object
ExpNumPerWLTh    7043 non-null object
Stair            7043 non-null object
ExpWLTh          7043 non-null object
ExpPer           7043 non-null object
dtypes: datetime64[ns](1), float64(3), int32(1), int64(2), object(8)
memory usage: 852.9+ KB

两个数据帧的示例数据 来自df\U new:

    File    Threshold   StepSize    RevNum  WL  RevPos  BkgdLt  Date    Person  AbRevPos    ExpNum  ExpNumPerWLTh   Stair   ExpWLTh ExpPer
7835    ZBL-2018-05-23_50_440   1   1.5 10.0    440 -12.012382  50  2018-05-23  ZBL 12.012382   Four    Four-ZBL-440-1  Four-ZBL-1  Four-440-1  Four-ZBL
7836    ZBL-2018-05-23_50_440   1   0.82    11.0    440 -13.512382  50  2018-05-23  ZBL 13.512382   Four    Four-ZBL-440-1  Four-ZBL-1  Four-440-1  Four-ZBL
7837    ZBL-2018-05-23_50_440   0   0.82    11.0    476 50.000000   50  2018-05-23  ZBL 50.000000   Four    Four-ZBL-476-0  Four-ZBL-0  Four-476-0  Four-ZBL
7838    ZBL-2018-05-23_50_440   0   1.5 12.0    476 50.000000   50  2018-05-23  ZBL 50.000000   Four    Four-ZBL-476-0  Four-ZBL-0  Four-476-0  Four-ZBL
7839    ZBL-2018-05-23_50_440   1   1.5 12.0    440 -11.052382  50  2018-05-23  ZBL 11.052382   Four    Four-ZBL-440-1  Four-ZBL-1  Four-440-1  Four-ZBL

从df\U val:

    Person  Threshold   WL  Threshold   Energy sum  White sum   Diff (energy)   Scaled energy   Sens (energy)   Sens (quanta)   Log sens (quanta)   Add 3   BkgdLt  BkgdLt_b    ExpNum
213 RJI 1   488 -30.224442  0.011540    0.013391    -0.001851   -185.08 -0.005403   -0.006422   -2.192351   0.807649    50  50  Four
214 SFO 0   488 28.068598   0.014332    0.013391    0.000941    94.12   0.010625    0.012628    -1.898674   1.101326    50  50  Four
215 SFO 1   488 -20.585589  0.012202    0.013391    -0.001189   -118.92 -0.008409   -0.009994   -2.000247   0.999753    50  50  Four
216 ZBL 0   488 30.690436   0.014410    0.013391    0.001019    101.88  0.009815    0.011666    -1.933081   1.066919    50  50  Four
217 ZBL 1   488 -30.671511  0.011497    0.013391    -0.001894   -189.40 -0.005280   -0.006275   -2.202372   0.797628    50  50  Four

首先用于导入一个.csv文件的代码:

# create data frame from values in csv file
df_val = pd.read_csv('Lum_Thresh_2_3_4.csv', sep=',', delimiter=None, header='infer', 
    names=['Person', 'Inc/dec (0 = inc)', 'Wavelength', 'Threshold', 'Energy sum', 'White sum', 
           'Diff (energy)', 'Scaled energy', 'Sens (energy)', 'Sens (quanta)', 'Log sens (quanta)', 
           'Add 3', 'BkgdLt_a'],
    engine='python', skiprows=1, infer_datetime_format=True)

Tags: 数据dfnewthresholdobjectnullenergyfour

热门问题