How do I convert a RangeIndex type to an Int64Index type? I have two dataframes, both imported from .csv files in the same way. Pandas automatically makes one an Int64Index and the other a RangeIndex. When I put the following code (to create a new column based on values in two other columns) in place for both dataframes, I get an error. I wanted to make both dataframes the same type so that my code would work for both dataframes to make new columns, which later I will use for a merge.
This code works for the Int64Index but not for the Range, and I confirmed that the relevant fields (columns) are the same in both dataframes.
This works great for the Int64Index dataframe (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)
Same code does not work in the RangeIndex dataframe (df_val), even though the datatypes for relevant columns are the same:
# create new column, combine 3 columns to make new one - for graphing
df_val['ExpWLTh']=df_val['ExpNum'].astype(str)+'-'+df_val['WL'].astype(str)+'-'+df_val['Threshold'].astype(str)
The RangeIndex dataframe (df_val) gives me this error when I try to create my new columns:
unorderable types: str() < int()
Here are the details for datatypes in each 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
Example data from both dataframes From df_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
From df_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
Code used to import one of the .csv files in the first place:
# 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)
This here works:
df_val.index = list(df_val.index)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With