简体   繁体   English

如何在熊猫python的数据框中插入行?

[英]How to insert rows into dataframes in pandas python?

I'm very new to pandas and dataframes. 我对熊猫和数据框非常陌生。

I have two lists in python which contains columns and values that needs to be inserted for that columns in a row. 我在python中有两个列表,其中包含列和需要为一行中的列插入的值。

For Ex: 例如:

col_list = ['column1', 'column3', 'column4', 'column6', ...]
val_list = ['value1', 'value3', 'value4', 'value6', ...]

How can I insert these values from val_list in columns in col_list for a specific row. 如何从val_list的col_list列中为特定行插入这些值。

And I need to iterate multiple times. 我需要重复多次。

These value and column list will change for every iteration but the order of column1 - value1, column2 - value2 doesn't change. 这些值和列列表将在每次迭代中更改,但column1-value1,column2-value2的顺序不变。

If the ith element in col_list is col-i , then ith element in val_list will be val-i . 如果col_list中的ith元素是col-i ,则val_list中的ith元素将是val-i

How can I enter data in only specific columns that are in col_list into pandas dataframe. 如何仅将col_list中特定列中的数据输入到pandas数据框中。

my_pd = pd.DataFrame(np.array(val_list)).T
my_pd.columns = col_list
pd.concat([a, my_pd], ignore_index=True)

I don't really see this in other answers, but here is a way to use concat to join your list to the end of the pandas dataframe . 我没有在其他答案中真正看到这一点,但是这是一种使用concat将列表加入到pandas dataframe末尾的方法。 It's tricky though, usually it's best to iterate through all the list you want to join, create a new DataFrame off that and then joining those 2 df together, it will be much easier and faster. 但是,这很棘手,通常最好遍历要加入的所有列表, DataFrame创建一个新的DataFrame ,然后将这两个df结合在一起,这将变得更加容易和快捷。

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

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