python - 如何将我的输出按列保存到 csv

标签 python pandas

这里,第一个函数必须返回 data.csv 文件中的第一个 col1,第二个函数必须返回第二列 col2。我写了一段代码但无法保存。

def func1(testvalue):
    json_d = {"task_id": "user_uid","data": {"document1":testvalue}} 
    response = requests.post("https://example.net/document",headers=headers,json=json_d)
    my_data1 = response.text
    file = open("data.csv","a+")
    file.write(my_data1)
    file.close()
    my_data = json.loads(my_data1)
    result = {'bool':my_data['data']}
    return result

def func2(testvalue):
    test = {"data": {"doc1":testvalue}} 
    response = requests.post("https://example.net/v3/docs",headers=headers,json=test)
    my_data2 = response.text
    file = open("data.csv","a+")
    file.write(my_data2)
    file.close()
    my_data = json.loads(my_data2)
    result = {'bool':my_data['data']}
    return result

df['res'] = df['test'].apply(func1) >> 它将把 file.write(my_data1) 保存到第一列并且 df['test'].apply(func1) 会将 file.write(my_data2) 保存到 data.csv 的第二列

最佳答案

解决方案

如果我正确理解您的要求,您希望将每一列保存为单独的 .csv 文件。您可以使用 pandas.DataFrame.to_csv 轻松完成此操作。

现在假设您希望一次将一列保存到一个.CSV 文件中。在这种情况下,您可以简单地将存储在您创建的每个文件中的列数据组合起来,如下所示,并将其保存为包含所有信息的单独 csv 文件。如果您愿意,您还可以在合并单个文件后将其删除。

Here we create a dummy dataframe with columns x and y and then write each of them to a .csv file of that column using pandas.DataFrame.to_csv as follows and finally check the list of files created at the output_path.

# Imports
import numpy as np
import pandas as pd
import os

# Make dummy dataframe
df = pd.DataFrame({'x': np.arange(10), 'y': np.arange(10)**2})

# Define your output_path
output_path = os.getcwd() + '/output'
if not os.path.exists(output_path):
    os.makedirs(output_path)

# Write individual CSV files for each column
for col_name in df.columns:
    df[col_name].to_csv(path_or_buf=os.path.join(output_path, col_name + '.csv'), index=False)

# Check/Enlist contents in the output-path
print(sorted(os.listdir(output_path)))

输出:

['x.csv', 'y.csv']

Reading in each column and together writing to a single CSV file

files = sorted(os.listdir(output_path))
df2 = pd.DataFrame()
for i, filename in enumerate(files):
    if filename not in ['out.csv']:
        col_name = filename.replace('.csv', '')  
        print('filename: {} | col_name: {}'.format(filename, col_name))  
        col_df = pd.read_csv(os.path.join(output_path, filename))    
        if i==0:
            df2.reindex(index = col_df.index)
        df2[col_name] = col_df['0']

# Write all columns to a single CSV
df2.to_csv(os.path.join(output_path, 'out.csv'))  

关于python - 如何将我的输出按列保存到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59463590/

相关文章:

python - 将列表拆分为列

python - 使用 Python Pandas 读取 .txt 文件 - 字符串和 float

python - 如何编写一个正则表达式来匹配一个字符串文字,其中转义是引号字符的两倍?

python - 绘制条形图 - ValueError : The truth value of a DataFrame is ambiguous. 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

python - PySpark 从本地函数广播变量

python - 查找多列总和最大的行

python - 用 Pandas 计算连胜纪录

python-3.x - 将字典键与数据框索引匹配的问题

python - 如何在 Python 中加入两个生成器?

python - Python 中的元组声明