{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# 定义要写入的数据\n", "data = [\n", " ['王五', '19', '河北', 'kk']\n", "]\n", "\n", "# 创建DataFrame对象\n", "df = pd.DataFrame(data, columns=['name', 'age', 'city', 'mark'])\n", "\n", "# 将DataFrame写入CSV文件\n", "df.to_csv('../data/llm_test.csv', index=False, mode='a', header=False)\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# 读取CSV文件到DataFrame\n", "df = pd.read_csv('../data/llm_test.csv')\n", "# 修改特定行列的数据\n", "row_index = 0 # 要修改的行索引,除去表头,从0开始\n", "column_name = 'age' # 要修改的列名\n", "new_value = '2345' # 修改后的值\n", "\n", "df.loc[row_index, column_name] = new_value\n", "# 将修改后的DataFrame写回到CSV文件\n", "df.to_csv('../data/llm_test.csv', index=True)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "name 2\n", "age 2\n", "city 2\n", "mark 2\n", "dtype: int64\n" ] } ], "source": [ "# 读取CSV文件到DataFrame\n", "df = pd.read_csv('../data/llm_test.csv')\n", "\n", "# 查找满足特定条件的行数据\n", "condition = df['age'] == 100 # 以Column1列为例,查找值为'Value1'的行\n", "filtered_data = df[condition]\n", "\n", "# 打印查找结果\n", "print(filtered_data)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " name age city mark\n", "0 张三 100 河北 kk\n", "1 赵四 19 河北 kk\n", "2 赵五 100 河北 kk\n" ] } ], "source": [ "df = pd.read_csv('../data/llm_test.csv').head(3)\n", "print(df)" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }