简体   繁体   English

如何在 python 中附加行时增加 pandas/csv 文件中列的值

[英]How to increment a value of column in pandas/csv file when the row is appended in python

I have this code which selects a column from a csv file and appends it as a row to another csv file:我有这段代码,它从 csv 文件中选择一列并将其作为一行附加到另一个 csv 文件:

def append_pandas(s,d):
    import pandas as pd
    df = pd.read_csv(s, sep=';', header=None)
    df_t = df.T
    df_t.iloc[0:1, 0:1] = 'Time Point'
    df_t.columns = df_t.iloc[0]
    df_new = df_t.drop(0)
    pdb = pd.read_csv(d, sep=';')
    newpd = pdb.append(df_new)
    from pandas import DataFrame
    newpd.to_csv(d, sep=';')

As you can see, there is a Time Point column, and every time the row is appended, I want the value in this column to increment by 1. For example, when the first row is appended, it is 0, the second row will have 1, the third row will have 3 etc.可以看到,有一个Time Point列,每追加一行,我想让这一列的值加1,比如追加第一行时为0,第二行会有 1 个,第三行将有 3 个,依此类推。

Could you please help with this?你能帮忙吗?

The resulting file looks like this:生成的文件如下所示:

在此处输入图像描述

PS The Row which is being appended doesn't have a Time Point value and looks like this: PS 附加的行没有时间点值,看起来像这样: 在此处输入图像描述

Please, help:(请帮忙:(

Try:尝试:

df1 = pd.read_csv('data1.csv')
df2 = pd.read_csv('data2.csv', index_col=0).T
df2['Time Point'] = df1['Time Point'].iloc[-1] + 1
out = pd.concat([df1, df2], ignore_index=True)
out.to_csv('out.csv', index=False)
print(out)

# Output
   Time Point    A   B
0           1   23  65
1           2   10  24
2           3    1  54
3           4   33  77
4           5    7  73
5           6  122  43  # <- row added with new Time Point

Setup设置

>>> %cat data1.csv
Time Point,A,B
1,23,65
2,10,24
3,1,54
4,33,77
5,7,73

>>> %cat data2.csv
ID,Count
A,122
B,43

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当在 python 中附加该行时,增加 pandas/csv 文件中列的值 - Increment a value of column in pandas/csv file when the row is appended in python 如何在Python中将csv文件行列的值转换为(行,列,值) - How can I turn csv file row column value into (row, column, value) in Python Python-有时会将CSV文件的附加行添加到前一行的末尾 - Python - Sometimes an appended row to a CSV file is added to the end of the previous row 使用 Python 从 CSV 文件中的指定行和列中提取值。 无法使用 CSV 模块或 pandas 模块 - Extract value from specified row and column in CSV file using Python. Cannot use CSV module or pandas module 如何在不使用 Pandas 的情况下在 Python 中逐行编辑 CSV 文件 - How to edit a CSV file row by row in Python without using Pandas Python:当一个键有多个值时,如何将字典写入csv文件,每个键是一个新行,但每个值是一个新列? - Python: How to write dictionary to csv file when one key has multiple values, each key is a new row, but each value is a new column? Python + Pandas:为csv文件中的特定行添加值 - Python + Pandas: Add value to specific row in csv-file 将列添加到CSV文件时追加了额外的行 - Extra row appended when adding columns to CSV file 如何在 pandas 的 csv 文件的特定列的行范围内插入文本? - How to insert text in a range of row in a specific column in a csv file in pandas? Python/Pandas:在基于 Excel 单元格的附加文件中创建列 - Python/Pandas: Create column in appended file based on Excel cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM