问:
怎么能让 AI 更好地分析我的结构化数据(Excel/CSV)?内容很长感觉就被丢了一些
答:
由于上下文窗口的限制,AI 无法一次性处理很长的内容。此外,大语言模型本身并不擅长数学运算,所以它也无法直接基于你提供的结构化数据去做计算。这时候最好的方法是结合代码使用。
比如,像 ChatGPT、Claude 这样的工具可以自动帮你生成代码来分析数据,甚至可以帮你分批处理数据。如果愿意,你也可以自己写代码实现分析。
基本原理
- 提供数据结构,不要给完整数据:在给 AI 的 prompt 中,只需要描述你数据的结构(比如列名和数据类型),或者给一个小的示例数据。不需要直接粘贴完整的数据,这样可以避免输入内容太长导致部分信息被丢失。
- 明确你的需求:清楚地告诉 AI 你想对数据进行什么操作或分析,比如生成统计表、绘图,或者计算某些指标。
- 让 AI 生成代码:基于你的需求,让 AI 写出代码来读取和分析数据。这样,代码可以处理你的完整数据并生成准确的结果,而不用担心上下文窗口限制。
示例
比如说,我把我 X 上的访问数据导出成 csv/excel 文件,包含每一天的访问量,然后我把这个数据提交给 ChatGPT(Claude 也可以),让它帮我:“分析我这一年的推文数据中,是哪个月访问量最大?哪一周访问量最大?哪一天访问两最大?”
ChatGPT 首先会对我的数据进行抽样,了解我的数据结构是什么样的。
然后根据需求去生成代码,最后运行代码获得结果。
Claude 也是类似,唯一的差别是 ChatGPT 用 Python,而 Claude 使用 JavaScript。
Claude 返回结果:
我来帮您分析这些数据。我会使用分析工具来处理CSV文件,查看不同时间维度的访问量(Profile visits)数据。 // 读取并解析CSV文件 const fileContent = await window.fs.readFile('account_overview_analytics 2.csv', { encoding: 'utf8' }); import Papa from 'papaparse'; const parsedData = Papa.parse(fileContent, { header: true, dynamicTyping: true, skipEmptyLines: true }); const data = parsedData.data; // 按月份分组 const monthlyVisits = {}; data.forEach(row => { const date = new Date(row.Date); const monthKey = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`; if (!monthlyVisits[monthKey]) { monthlyVisits[monthKey] = 0; } monthlyVisits[monthKey] += row['Profile visits'] || 0; }); // 找出访问量最大的月份 const maxMonthVisits = Object.entries(monthlyVisits) .sort((a, b) => b[1] - a[1])[0]; console.log('月度访问量最高:', maxMonthVisits); // 按周分组 const weeklyVisits = {}; data.forEach(row => { const date = new Date(row.Date); const weekNumber = getWeekNumber(date); const weekKey = `${date.getFullYear()}-W${String(weekNumber).padStart(2, '0')}`; if (!weeklyVisits[weekKey]) { weeklyVisits[weekKey] = 0; } weeklyVisits[weekKey] += row['Profile visits'] || 0; }); // 找出访问量最大的周 const maxWeekVisits = Object.entries(weeklyVisits) .sort((a, b) => b[1] - a[1])[0]; console.log('周访问量最高:', maxWeekVisits); // 找出访问量最大的天 const dailyVisits = data.map(row => ({ date: row.Date, visits: row['Profile visits'] || 0 })); const maxDayVisits = dailyVisits.sort((a, b) => b.visits - a.visits)[0]; console.log('日访问量最高:', maxDayVisits); // 辅助函数:获取日期所在的周数 function getWeekNumber(date) { const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate())); const dayNum = d.getUTCDay() || 7; d.setUTCDate(d.getUTCDate() + 4 - dayNum); const yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1)); return Math.ceil((((d - yearStart) / 86400000) + 1)/7); } 根据分析结果: 1. 访问量最高的月份是 2024年2月,总访问量达到了 25,317 次 2. 访问量最高的周是 2024年第7周(2月中旬),访问量为 9,537 次 3. 访问量最高的单日是 2024年2月16日(星期五),当天访问量达到了 4,203 次 看起来2月份是您账号表现最好的时期,特别是2月中旬的那一周。您是否记得在2月16日发布了什么特别的内容,导致访问量激增?这可能对您未来的内容策略有所启发。 如果您想了解更多细节,比如具体的内容类型分析或者其他指标(如互动率、关注率等),我也可以为您进一步分析。
ChatGPT:
import pandas as pd # Load the CSV file to analyze the data file_path = '/mnt/data/account_overview_analytics (2).csv' data = pd.read_csv(file_path) # Display the first few rows to understand the structure of the data data.head() Result: Result Date Impressions Likes Engagements Bookmarks Shares 0 Fri, Dec 27, 2024 17955 65 405 29 1 1 Thu, Dec 26, 2024 1126219 2870 21818 1386 65 2 Wed, Dec 25, 2024 1657311 4312 19589 1544 78 3 Tue, Dec 24, 2024 401326 1424 12266 749 41 4 Mon, Dec 23, 2024 467837 1528 16703 782 43 New follows Unfollows Replies Reposts Profile visits Create Post 0 4 0 10 10 35 0 1 206 39 198 427 1099 1 2 361 23 179 667 1700 3 3 141 15 153 272 517 5 4 96 27 217 260 559 2 Video views Media views 0 242 346 1 22707 88041 2 12096 23161 3 17368 42005 4 34821 77995 The dataset includes columns such as Date, Impressions, Likes, and others. To answer your questions regarding: 1. Month with the maximum impressions: I'll group the data by month and calculate total impressions. 2. Week with the maximum impressions: I'll group the data by week and calculate total impressions. 3. Day with the maximum impressions: I'll find the single date with the highest impressions. Let me proceed with the calculations. # Convert the 'Date' column to a datetime format for easier grouping data['Date'] = pd.to_datetime(data['Date'], format='%a, %b %d, %Y') # Add helper columns for month and week data['Month'] = data['Date'].dt.to_period('M') data['Week'] = data['Date'].dt.isocalendar().week # Group by month and sum the impressions monthly_impressions = data.groupby('Month')['Impressions'].sum() # Group by week and sum the impressions weekly_impressions = data.groupby('Week')['Impressions'].sum() # Find the day with the maximum impressions max_day = data.loc[data['Impressions'].idxmax()] # Extract the results max_month = monthly_impressions.idxmax() max_month_value = monthly_impressions.max() max_week = weekly_impressions.idxmax() max_week_value = weekly_impressions.max() max_day_date = max_day['Date'] max_day_value = max_day['Impressions'] (max_month, max_month_value, max_week, max_week_value, max_day_date, max_day_value) Result (Period('2024-02', 'M'), 22484999, 7, 7480737, Timestamp('2024-02-16 00:00:00'), 2753640) Here is the analysis of your tweet impressions: 1. Month with the maximum impressions: February 2024, with a total of 22,484,999 impressions. 2. Week with the maximum impressions: Week 7 (mid-February 2024), with a total of 7,480,737 impressions. 3. Day with the maximum impressions: February 16, 2024, with 2,753,640 impressions. Let me know if you'd like a more detailed visualization or further breakdown!
如果你无法访问上述AI工具,可以尝试使用国内同类产品:智谱清言 商汤商量 ,都有面向EXCEL文件数据分析的AI对话工具。