- 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"]]
3 Respostas
+ 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.
0
Monika Raut Simply copying the information by hand isn’t good practice. What happens the next time a user gets a data file?
- 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))