I have a csv file that has rows xy and z and columns of different names. Essentially these are three-dimensional coordinates of each name. I have imported this csv as a dataframe which looks like:
Coordinate C1 C2 C3 C4 C5 N6
0 x 0.16620 0.20640 0.16240 0.08140 0.04370 0.08288
1 y 0.22340 0.34680 0.44090 0.41100 0.28550 0.18996
2 z 0.38187 0.42618 0.40091 0.33013 0.28793 0.31430
I would like to perform transformations to the coordinates such as (x,y,z)->(y,x,z). I don't have much experience with python but I find I can do this with a simple function such as
def trans(x,y,z):
return (y,x,z)
I am having issues with getting the values from each column in the form of x, y, and z so that I may pass them through the function to achieve the desired transformation. I find that I can convert each column to a tuple by using
tuple([tuple(co_df[col]) for col in co_df])
however, this will ultimately give me a tuple of tuples. I could instead turn this into a list of tuples, but then I have the issue of passing a list of tuples into my function to transform the coordinates. Any help is appreciated!
I don't have enough reputation to comment yet, but maybe a for loop would be useful
For example, to print the output of trans for each column, you could do something like this:
for column in list(co_df):
a,b,c=co_df[column]
print(trans(a,b,c))
This would print:
('y', 'x', 'z')
(0.22340, 0.16620, 0.38187)
(0.34680, 0.20640, 0.42618)
(0.44090, 0.16240, 0.40091)
(0.41100, 0.08140, 0.33013)
(0.28550, 0.04370, 0.28793)
(0.18996, 0.08288, 0.31430)
What are you planning on doing with the values once they been transformed? Do you need them stored in an object?
EDIT Re. your question about what a,b,c=co_df[column]
does, think about this at a single column level eg co_df["C1"]
print(co_df["C1"])
returns
0 0.16620
1 0.22340
2 0.38187
Doing a,b,c=co_df["C1"]
assigns each value of co_df["C1"]
to a, b, and c respectively.
Here is the original data frame:
from io import StringIO
import pandas as pd
data = ''' Coordinate C1 C2 C3 C4 C5 N6
0 x 0.16620 0.20640 0.16240 0.08140 0.04370 0.08288
1 y 0.22340 0.34680 0.44090 0.41100 0.28550 0.18996
2 z 0.38187 0.42618 0.40091 0.33013 0.28793 0.31430
'''
df = pd.read_csv(StringIO(data), sep='\s+')
print(df)
Coordinate C1 C2 C3 C4 C5 N6
0 x 0.16620 0.20640 0.16240 0.08140 0.04370 0.08288
1 y 0.22340 0.34680 0.44090 0.41100 0.28550 0.18996
2 z 0.38187 0.42618 0.40091 0.33013 0.28793 0.31430
First, you can put the columns in a different order like this:
print(df[['Coordinate', 'N6', 'C5', 'C4', 'C3', 'C2', 'C1']])
Coordinate N6 C5 C4 C3 C2 C1
0 x 0.08288 0.04370 0.08140 0.16240 0.20640 0.16620
1 y 0.18996 0.28550 0.41100 0.44090 0.34680 0.22340
2 z 0.31430 0.28793 0.33013 0.40091 0.42618 0.38187
Second, you can re-label the columns like this (assign to df.columns):
df.columns = ['Coordinate', 'N6', 'C5', 'C4', 'C3', 'C2', 'C1']
print(df)
Coordinate N6 C5 C4 C3 C2 C1
0 x 0.16620 0.20640 0.16240 0.08140 0.04370 0.08288
1 y 0.22340 0.34680 0.44090 0.41100 0.28550 0.18996
2 z 0.38187 0.42618 0.40091 0.33013 0.28793 0.31430
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.