【问题标题】:Using a loop to populate rows of a new column in python based on content found in other column rows使用循环根据在其他列行中找到的内容在 python 中填充新列的行
【发布时间】:2020-11-10 22:46:53
【问题描述】:

我正在寻求有关在我的数据框中填充新列的内容的帮助。我需要根据其他列中的内容填充 New_column。

import pandas as pd

df = pd.read_csv('sample.txt')
# the data is imported as one column
df.columns = ['Test']
# split into columns
dfnew = df.Test.str.split(expand=True).rename(columns={0:'Datetime', 1:'P1', 2:'P2'})

# create a new column
dfnew["New_column"] = ""
print(dfnew)

                Datetime     P1           P2        New_column
8             'Name-1'      None         None          
9    2017-01-01T00:00:00    2800         1600          
10   2017-02-01T00:00:00  -99999         2375            
..                   ...     ...          ...       ...
72            'Name-2'      None         None         
73   2018-10-11T00:00:00     0           2000          
74   2018-10-18T00:00:00     0           2000                  
..                   ...     ...          ...       ...
[724 rows x 4 columns]

在 .txt 文件中,当 Datetime 列中有 Name-# 值时,P1 和 P2 行为空白,但在打印 df 时,空白被替换为“None”。每 x 行,Datetime 列中的 Name-# 更改(与名称关联的数字不会按任何顺序增加)。我希望 New_column 使用在 Datetime 列中找到的 Name-# 填充每一行,直到下一个 Name-# 值替换它:

                Datetime     P1           P2        New_column
8             'Name-1'       None         None          
9    2017-01-01T00:00:00     2800         1600          Name-1
10   2017-02-01T00:00:00   -99999         2375          Name-1
..                   ...     ...          ...       ...
72            'Name-2'       None         None         
73   2020-10-11T00:00:00      0           2000          Name-2
74   2020-10-18T00:00:00      0           2000          Name-2       
..                   ...     ...          ...       ...
623           'Name-14'      None         None         
624  2020-04-21T00:00:00   -99999         730           Name-14
625  2020-04-27T00:00:00      0           260           Name-14
..                   ...     ...          ...       ...
[724 rows x 4 columns]

我还想删除 Datetime 列中包含 Name-# 的行(即第 8 行、第 72,623 行等)。我需要使这个过程自动化,以便我可以导入相同样式但不一定具有相同大小或相同 Name-# 值的 .txt 文件。我尝试使用带有多个 if 语句的 for 循环创建一个列表,然后将 New_column 分配给该列表,但我似乎无法让它工作..

我是 Python 的初学者,非常感谢任何帮助。

【问题讨论】:

    标签: python dataframe loops validation multiple-columns


    【解决方案1】:

    试试下面的代码。 首先,我们使用 Datetime 列创建一个新列。 如果列值包含“名称”,则“new_col”的值将是 DateTime 列的值,否则它将是 np.NaN(相当于 NULL)。

    如果值为 np.nan,我们使用 ffill() 函数向前填充 new_col。

    import numpy as np
    dfnew['new_col']=[x if 'Name' in str(x) else np.nan for x in  dfnew.Datetime.values ]
    dfnew['new_col']=dfnew['new_col'].ffill()
    
    

    [ffill()][1]

    [1]:https://www.geeksforgeeks.org/python-pandas-dataframe-ffill/#:~:text=ffill()%20function%20is%20used,propagate%20last%20valid%20observation%20forward.&text=inplace%20%3A%20If%20True%2C%20fill%20in,a%20column%20in%20a%20DataFrame)。

    【讨论】:

    • 嗨@Amit,我想知道是否可以做相反的事情?例如,每次“name-#”在另一列中发生变化时,取出一个充满不同名称的列并创建一个仅包含“name-#”的新行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2021-08-31
    • 2019-09-30
    相关资源
    最近更新 更多