- 1

Adding two column in Pandas data frame.

Hi, I am new in Pandas and trying to add two columns and want to create a new text file with the average value of n_top with n_bottom and k_top and k_bottom columns. When I am adding the two-column (column contains float numbers), it is considering them as a string, not a number. If I try to change int(df[]), it doesn't work. How to add two columns in pandas easily. import pandas as pd filename = r'C:\Users\vashisv1\My local data\SLM Design\ITO_refractive_index_524nm.txt' df = pd.read_csv(filename, skiprows=1, sep = '\t', names=["lambda(nm)", "n -Top", "k -Top", "n -Bottom", "k -Bottom"]) df.drop(0, inplace = True) print(df.head(5)) n_avg = [df["n -Top"] + df["n -Bottom"]]

20th Oct 2020, 9:23 AM
Vishal Vashistha
Vishal Vashistha - avatar
3 Answers
+ 1
When you read a text file, the stuff will come in as strings, but you can convert it to numbers with the int() or float() functions. How to do this with multiple values within a pandas data frame, I’m not sure.
11th Apr 2021, 11:30 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Monika Raut Simply copying the information by hand isn’t good practice. What happens the next time a user gets a data file?
6th Jul 2022, 8:49 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
- 1
products <- data.frame( "id" = seq(1:8), "price" = c(8000, 3500, 1200, 19000, 5000, 4800, 6700, 9300), "tax" = c(300, 400, 200, 500, 250, 200, 550, 400)) products$total<- products$price + products$tax print(mean(products$total))
6th Jul 2022, 7:24 AM
Monika Raut
Monika Raut - avatar