用python修改cvs数据

vyswwuz2  于 2023-05-21  发布在  Python
关注(0)|答案(1)|浏览(120)

嗨,伙计们,我希望你们做得很好。
我有一些问题与数据库,是因为他们的方式是一致的,使它不可能产生一个报告或分析他们
这是一个例子
Example
如果您看到i cannot sort it,则t具有变量,因为某些行没有信息,如果这些行消失,则没有关系
为了工作的文件,我需要看到它像这样
Result file
我很抱歉,我只是一个前端开发人员,我有一个真正的困难时间的问题,如果有人可以帮助我,我会很感激
我共享的文件不是敏感信息
这是文件csv https://drive.google.com/file/d/1UbhuyUYhQRu68XvqMuiac0nlZhCQoq_t/view?usp=sharing的示例

z2acfund

z2acfund1#

下面是一段代码,它使用pandas库将您共享的csv转换为您共享的第二个屏幕截图的格式:

import pandas as pd

# Read in the csv
df = pd.read_csv('testalan.csv', header=None, names=['Machine', 'TempHeader'])

# Rename the temporary header to the metadata entry in the first row
df.rename(
    columns={
        'TempHeader': df.iloc[0].Machine
    },
    inplace=True
)

# Drop the first row that is now part of the header
df = df[1:]

# Drop all rows missing at least one element
df = df.dropna(axis='rows')

# Transpose the matrix to flip the rows and columns. Could replace df.transpose() with df.T
df = df.transpose()

print(df)

如果你还想按最下面的一行排序,你可以添加这行:

# Get the name of the 2nd row and sort the columns by the value of that row
df = df.sort_values(by=df.iloc[1].name, axis='columns')

下载csv:

df.to_csv('testalan_sorted.csv', header=False)

编辑:
我假设csv的格式是一行MachineName,后面跟一个条目,表示该机器的每个属性。下面是一段python代码,它将根据这个假设和您在注解中提供的描述解析csv文件:

import pandas as pd

# Read the original CSV file
df = pd.read_csv('testalan.csv', header=None)

# Initialize an empty dictionary to store the data
data = {}

# Loop through the rows of the DataFrame
for _, row in df.iterrows():
    if '(' in row[0]:
        machine_name = row[0]
        data[machine_name] = {}
    else:
        column_name, column_value = row[0], row[1]
        data[machine_name][column_name] = column_value

# Convert the dictionary to a DataFrame
result = pd.DataFrame.from_dict(data, orient='index')

# Save the result to a new CSV file
result.to_csv('output.csv', index_label='Machine')

编辑二:
此代码将允许重复的计算机。它将为每台计算机附加一个新行,而不管为该计算机定义了多少属性:

import pandas as pd

# Read the original CSV file
df = pd.read_csv('original file.csv - Hoja 1.csv', header=None)

# Initialize an empty dataframe to store the resulting data
result = pd.DataFrame()
machine_name = None

# Loop through the rows of the DataFrame
for _, row in df.iterrows():
    if '(' in row[0]:
        # Every time you switch to a new machine, add the current machine to the result set
        if machine_name is not None:
            machine_entries = pd.DataFrame.from_dict({ machine_name: machine_attributes }, orient='index')
            result = pd.concat([result, machine_entries], ignore_index=False)

        # Update the current machine name and clear the machine attributes
        machine_name = row[0]
        machine_attributes = {}
    else:
        column_name, column_value = row[0], row[1]
        machine_attributes[column_name] = column_value

# Save the result to a new CSV file
result.to_csv('output.csv', index_label='Machine')

相关问题