亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重復計算的最佳實踐

重復計算的最佳實踐

largeQ 2023-06-20 16:27:19
機器中的齒輪:數據包含當前 12 個月的數據并水平堆疊。每個月都會修改更新并附加新的月份。  ID  |Date        |Month1_a |Month1_b |Month1_c |Month2_a |Month2_b |Month2_c |Month3_a |Month3_b |Month3_c  ##  |MM/DD/YYYY  |abc      |zxy      |123      |NULL     |zxy      |122      |abc      |zxy      |123實際數據文件沒有標題,并作為每月第 1 個月的不同文件等在下游攝取。ID  | Date       |Month1_a |Month1_b |Month1_c |New Column##  |MM/DD/YYYY  |abc      |zxy      |123      | #ID  | Date       |Month2_a  |Month2_b |Month2_c |New Column##  |MM/DD/YYYY  |NULL      |zxy      |122      | #除了復制文件 12 次。是否有任何關于閱讀一次并循環創建我的輸出的建議。我已經制定了第 1 個月的邏輯,我對如何進入第 2 個月以上感到困惑。最初是想讀取文件 > 刪除 Month 3+ > Drop Month 1 > Run Logic,但我不確定是否有更好/最佳實踐。謝謝。
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

這將輸出 n 個 csv 文件,其中 n 是輸入數據中的月數。希望這就是您所追求的。


import pandas as pd


df = pd.read_csv('my_data.csv', sep='|')


# Strip whitespace from column names

df.columns = [x.strip() for x in df.columns]


# Get a set of months in the data by splitting on _ and removing 'Month' from

# the first part

months = set([x.split('_')[0].replace('Month','') for x in df.columns if 'Month' in x])


# For each numeric month in months, add those columns with that number in it to

# the ID and Date columns and write to a csv with that month number in the csv title

for month in months:

    base_columns = ['ID','Date']

    base_columns.extend([x for x in df.columns if 'Month'+month in x])

    df[base_columns].to_csv(f'Month_{month}.csv', index=False)


查看完整回答
反對 回復 2023-06-20
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號