简体   繁体   English

Python:在CSV的列上执行功能并将该CSV附加

[英]Python: Performing a function on a column in a CSV and appending that CSV

I created a function that parses a filename into its constituent parts, including camera information and a time stamp. 我创建了一个函数,将文件名解析为其组成部分,包括相机信息和时间戳。 I want to preform this function (and the only part that is relevant to me is the time stamp so that is what I want to return) on a column of a CSV that contains the filename in its first column. 我想在CSV的一列上执行此功能(与我有关的唯一部分是时间戳,这就是我要返回的内容),该CSV的第一列中包含文件名。

    Exx, mean   filename
0   1.14E-33    cam0_006806_418.852.csv
1   4.54E-05    cam0_006807_418.910.csv
2   4.48E-05    cam0_006808_418.975.csv
3   0.000138274 cam0_006809_419.037.csv
4   0.000118886 cam0_006810_419.097.csv
5   0.001155703 cam0_006811_419.157.csv

I want to add the parsed time to a fourth column. 我想将解析的时间添加到第四列。 This is what I have so far 这就是我到目前为止

def csvdecode(f):

    s = os.path.basename(f)
    pattern = "".join([r'cam(?P<cam_id>[0-9]+)_',
                    r'(?P<frame_id>[0-9]+)_'
                    r'(?P<time>[0-9]+.[0-9]+)'])
    m = re.search(pattern, s)
    d = {'Camera ID': m.group('cam_id'),
        'Frame ID': m.group('frame_id'),
        'Timestamp (s)': float(m.group('time'))}
    return d['Timestamp (s)']
    # this returns only the "time" portion of the timestamp

 df = pd.read_csv('results_avg_optical_strain.csv')
 df['Time (s)'] = df['filename'].apply(csvdecode)

and it runs with no errors but nothing is added to the existing csv. 并且它运行没有错误,但是没有任何内容添加到现有的csv中。 Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

To add a column to a .csv using pandas all you need to do is 要使用熊猫将列添加到.csv,您要做的就是

df['column name'] = 'something'

This will automatically update your .csv to include that column. 这将自动更新您的.csv以包括该列。 It will populate the value so that its length matches the length of the other columns in your .csv 它将填充值,使其长度与.csv中其他列的长度匹配

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

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