简体   繁体   English

在 function 中将 pandas 列的值作为单独的 arguments 传递

[英]Pass values of a pandas column as separate arguments in a function

I have a csv file that has rows xy and z and columns of different names.我有一个 csv 文件,其中包含 xy 和 z 行以及不同名称的列。 Essentially these are three-dimensional coordinates of each name.本质上,这些是每个名称的三维坐标。 I have imported this csv as a dataframe which looks like:我已将此 csv 作为 dataframe 导入,如下所示:

 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).我想对坐标进行转换,例如 (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我对 python 没有太多经验,但我发现我可以用一个简单的 function 来做到这一点,例如

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.我在以 x、y 和 z 的形式从每列获取值时遇到问题,以便我可以将它们传递给 function 以实现所需的转换。 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.我可以改为将其转换为元组列表,但随后我遇到了将元组列表传递到我的 function 以转换坐标的问题。 Any help is appreciated!任何帮助表示赞赏!

I don't have enough reputation to comment yet, but maybe a for loop would be useful我还没有足够的声誉来发表评论,但也许 for 循环会很有用

For example, to print the output of trans for each column, you could do something like this:例如,要为每一列打印 trans 的 output,您可以执行以下操作:

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?您是否需要将它们存储在 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您关于a,b,c=co_df[column]做什么的问题,请在单个列级别考虑这一点,例如co_df["C1"] print(co_df["C1"])返回

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.执行a,b,c=co_df["C1"]co_df["C1"] ["C1"] 的每个值分别分配给 a、b 和 c。

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):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM