简体   繁体   English

将 function 传递到数据框中的列 - Python

[英]Pass function to column in data frame - Python

I am trying to pass a function that truncates a timestamp to a single column.我正在尝试传递将时间戳截断为单个列的 function。 It's performing the function but returning a list.它正在执行 function 但返回一个列表。 I'm hoping to keep the data structure.我希望保留数据结构。

df = pd.DataFrame({
    'Time' : ['8:03:001','8:17:004','8:20:003','8:28:002','8:35:004','8:40:006','8:42:002','8:45:004','8:50:009'],                 
    'Place' : ['House 1','House 1','House 1','House 2','House 2','House 2','House 3','House 3','House 3'],                 
     })

def truncate_time(col):
    col = [x[:-2] for x in col]
    return col

df1 = (truncate_time(df['Time']))

Intended Output:预期 Output:

       Time    Place
0  8:03:0    House 1
1  8:17:0    House 1
2  8:20:0    House 1
3  8:28:0    House 2
4  8:35:0    House 2
5  8:40:0    House 2
6  8:42:0    House 3
7  8:45:0    House 3
8  8:50:0    House 3

You can assign back:您可以分配回:

df['Time'] = truncate_time(df['Time'])
print (df)
     Time    Place
0  8:03:0  House 1
1  8:17:0  House 1
2  8:20:0  House 1
3  8:28:0  House 2
4  8:35:0  House 2
5  8:40:0  House 2
6  8:42:0  House 3
7  8:45:0  House 3
8  8:50:0  House 3

But here is also possible use str with indexing:但这里也可以将str与索引一起使用:

df['Time'] = df['Time'].str[:-2]

Or lambda function:或 lambda function:

df['Time'] = df['Time'].apply(lambda col: col[:-2])

Or for function simplify solution with remove list comprehension with Series.apply :或者对于 function 简化解决方案,使用Series.apply删除列表理解:

def truncate_time(col):
    return col[:-2]

df['Time'] = df['Time'].apply(truncate_time)

And last solution with list comprehension:最后一个带有列表理解的解决方案:

df['Time'] = [x[:-2] for x in df['Time']]

EDIT: Performance with possible missing values - depends of number of values and also number of missing values:编辑:可能缺失值的性能 - 取决于值的数量以及缺失值的数量:

#added one row with missing value
df = pd.DataFrame({
    'Time' : ['8:03:001','8:17:004','8:20:003','8:28:002','8:35:004','8:40:006','8:42:002','8:45:004','8:50:009',np.nan],                 
    'Place' : ['House 1','House 1','House 1','House 2','House 2','House 2','House 3','House 3','House 3','House 3'],                 
     })

def truncate_time(col):
    return col[:-2] if col == col else col

#[1000000 rows x 2 columns]
df = pd.concat([df] * 100000, ignore_index=True)

In [104]: %timeit df['Time1'] = df['Time'].str[:-2]
460 ms ± 20.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [105]: %timeit df['Time2'] = [x[:-2] if x == x else x for x in df['Time']]
445 ms ± 9.72 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [106]: %timeit df['Time3'] = df['Time'].apply(lambda col: col[:-2] if col == col else col)
428 ms ± 18.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [107]: %timeit df['Time4'] = df['Time'].apply(truncate_time)
416 ms ± 8.28 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

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

相关问题 Python:如何在数据框中传递3列作为函数中的3个独立参数并迭代列值 - Python: how to pass 3 columns in data frame as 3 separate arguments in function and iterate through the column values python pandas 在 groupby 中应用 function,并将结果添加为数据框中的列 - python pandas apply function in groupby, and add results as column in data frame 如何通过函数传递数据帧并返回另一个数据帧? - How to pass a data frame through a function and return another data frame? 如何将列从数据帧传递到NLTK Python中的wordnet.synsets() - How to pass a column from a data frame into wordnet.synsets() in NLTK python 从 Python 中的数据框中的列中提取数据 - Extracting data from a column in a data frame in Python 带有条件数据的数据框列-Python - Data frame column with conditional data - Python 在 python 中创建一个带有“while”循环的 Function 并使用 function 在数据框中生成一个新列 - Create a Function with 'while' loop in python and use the function to generate a new column in a data frame 将年龄函数应用于数据框列 - Applying age function to data-frame column Function 使用可变文本清理数据框列 - Function to clean a Data Frame column with variable text 将特定的 function 应用于 pandas 中数据框的列 - Apply a specific function to a column of a data frame in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM