gpt4 book ai didi

Python 数据帧复制

转载 作者:太空宇宙 更新时间:2023-11-04 02:49:06 25 4
gpt4 key购买 nike

我正在尝试根据基于原始数据框的某些标准创建新数据框。

df = pandas.io.sql.read_sql(sql, conn)

Count_Row = df.shape[0]
for j in range(Count_Row - 1):

if df.iloc[j, 0] == df.iloc[j + 1, 0]:
print(df.iloc[j, 2] + df.iloc[j + 1, 2], df.iloc[j, 4], df.iloc[j, 6], df.iloc[j, 3])

然而,我不想打印,而是想将该数据添加到新的数据框中。

这怎么可能?

最佳答案

您可以将其附加到新的数据框,而不是打印出数据

import pandas as pd

df = pandas.io.sql.read_sql(sql, conn)
Count_Row = df.shape[0]

results = pd.DataFrame() # create data frame to store results

for j in range(Count_Row - 1):
if df.iloc[j, 0] == df.iloc[j + 1, 0]:
# create row of values to append
row = pd.Series([df.iloc[j, 2] + df.iloc[j + 1, 2],
df.iloc[j, 4],
df.iloc[j, 6],
df.iloc[j, 3]])
results = results.append([row])

results.columns = ['v1', 'v2', 'v3', 'v4'] # the variables

这将为您提供具有所需输出的数据框

关于Python 数据帧复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44369389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com