简体   繁体   English

如何用变量函数迭代地填充 pandas dataframe 中的行

[英]How to fill rows in pandas dataframe with variable functions iteratively

I want to add new rows in DataFrame pandas each time I run the program I create.每次运行我创建的程序时,我想在 DataFrame pandas 中添加新行。 I don't know the data in advance, the functions are supposed to put the data in a variable and I want to add these variables in a row.我事先不知道数据,函数应该将数据放在一个变量中,我想将这些变量连续添加。 For now I just success to add one row, but when I run the program each time this row is replace by the next one.现在我只是成功地添加了一行,但是当我每次运行程序时,这一行都会被下一行替换。 I don't want the row to be replaced but added in the next row.我不想替换该行,而是添加到下一行。

net_index = mylist.index('NET PAYE EN EUROS ')
net= mylist[net_index+2]  

total_index= mylist.index('CONGES ')
total = (mylist[total_index-1])


df = pd.DataFrame(columns=['Mois','Nom','Adresse','Net_payé','Total_versé'])
new = {'Mois': mois, 'Nom': nom, 'Adresse': adresse,'Net_payé':net, 'Total_versé':total}
df= df.append(new, ignore_index=True)

This is a part of my code.这是我的代码的一部分。 First I create an empty Dataframe with name of columns, and then a dict with variables which are supposed to change for each run.首先,我创建一个带有列名称的空 Dataframe,然后创建一个包含每次运行都会更改的变量的字典。

This is the result I have, but each time I run, the rows is replace by the next one, and not add这是我得到的结果,但是每次运行时,行都会被下一个替换,而不是添加

I suppose I have to do a loop, but it never works well, I search everywhere for a solution but don't find one.我想我必须做一个循环,但它永远不会很好,我到处寻找解决方案,但没有找到。

So do you know what can I do?所以你知道我能做什么吗? Thank you so much太感谢了

Apparently, you are not saving the dataframe anywhere.显然,您没有将 dataframe 保存在任何地方。 Once your program exits, all data and variables are erased (lost).一旦您的程序退出,所有数据和变量都会被擦除(丢失)。 You cannot retrieve data from a previous run.您无法从以前的运行中检索数据。 The solution is to save the dataframe into a file before exiting your program.解决方案是在退出程序之前将 dataframe 保存到文件中。 Then for each run, load the previous data from file.然后对于每次运行,从文件中加载以前的数据。

Actually yes I save the dataframe in a csv file.实际上是的,我将 dataframe 保存在 csv 文件中。 Because my goal is to implement the variables's results in a csv.因为我的目标是在 csv 中实现变量的结果。 But the result is the same as I show before, always take the first row and replace it, not add new one.但结果和我之前展示的一样,总是取第一行并替换它,而不是添加新的。

df = pd.DataFrame(columns=['Mois','Nom', 'Adresse','Net_payé','Total_versé'])
new = {'Mois': mois, 'Nom': nom, 'Adresse': adresse,'Net_payé':net, 'Total_versé':total}
df =df.append(new, ignore_index=True)
    
df.to_csv('test.csv', header=True, index=False, encoding='utf-8')

Thanks for your reply!感谢您的回复!

There are multiple ways to add rows to an existing DataFrame.有多种方法可以向现有 DataFrame 添加行。 One way is to usepd.concat , of which the df.append function on the last code line in your questions is a specific use case .一种方法是使用pd.concat ,其中问题中最后一行代码中的df.append function是一个特定的用例

However, the method I prefer is to create a nested list that contains my data, and then create a new DataFrame from scratch.但是,我更喜欢的方法是创建一个包含我的数据的嵌套列表,然后从头开始创建一个新的 DataFrame。 First make sure all the variables you want to place in the columns are lists of the same length.首先确保要放置在列中的所有变量都是相同长度的列表。 Something like this (lists with a length of 2 in this example):像这样的东西(本例中长度为 2 的列表):

mois_data = [1,2]
nom_data = [3,4]
adresse_data = [5,6]
net_paye_data = [7,8]
total_verse_data = [9,10]

Place this data in a dictionary.将此数据放入字典中。 Make sure to set the columns names of your DataFrame as the keywords (note: this might cause problems with the accent aigu you're using in some of the variable names, To be sure. I'm omitting these. You can later rename them including the accent aigu using the rename-function ).确保将 DataFrame 的列名称设置为关键字(注意:这可能会导致您在某些变量名称中使用的重音 aigu 出现问题,可以肯定的是。我省略了这些。您可以稍后重命名它们包括使用rename-function的重音 aigu )。

data_dict = dict(Mois=mois_data, Nom=nom_data, Adresse=adresse_data, Net_paye=net_paye_data, Total_verse=total_verse_data)

Then create the dataframe, using the dictionary as data input:然后创建 dataframe,使用字典作为数据输入:

df = pd.DataFrame(data=data_dict, columns=['Mois','Nom','Adresse','Net_paye','Total_verse'])

Which results in:结果是:

   Mois  Nom  Adresse  Net_paye  Total_verse
0     1    3        5         7            9
1     2    4        6         8           10

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM