简体   繁体   English

如何将列表/数组中的列填充到空的 Pandas dataframe 中,只有列名

[英]How to fill in columns from lists/arrays into an empty Pandas dataframe with only column names

I have a dataframe with several column names.我有一个 dataframe 有几个列名。 As and when I get data for each column, I have to create rows.当我获取每一列的数据时,我必须创建行。 I don't have all the row data available in one place.我没有在一个地方提供所有行数据。 As and when I get data for a column in a particular row, I will fill it当我获取特定行中的列的数据时,我将填充它

In this example below, I have created an empty dataframe and and I am trying to fill in a particular column with a set of values.在下面的这个例子中,我创建了一个空的 dataframe 并且我试图用一组值填充一个特定的列。 This is not working.这是行不通的。

import pandas as pd
import numpy as np

col_names = ['ampere', 'freq', 'count']
dataf = pd.DataFrame(columns = col_names)
freq = np.arange(0.6,2.6,0.1).tolist()
#Add the list of frequencies to the frequency column
dataf['freq'].iloc[1:len(freq)] = freq

The error I am getting is我得到的错误是

ValueError: cannot copy sequence with size 20 to array axis with dimension 0 ValueError:无法将大小为 20 的序列复制到维度为 0 的数组轴

You're almost there!您快到了! There is no need for the iloc -indexing.不需要iloc索引。

So just change your last line to所以只需将您的最后一行更改为

dataf['freq'] = freq

and it should work as expected.它应该按预期工作。

If you want to keep adding more rows from different lists, as you mentioned in the comment, you can use following code snippet:如果您想继续从不同的列表中添加更多行,正如您在评论中提到的,您可以使用以下代码片段:

import pandas as pd
import numpy as np

col_names = ['ampere', 'freq', 'count']
dataf = pd.DataFrame(columns = col_names)
freq = np.arange(0.6,2.6,0.1).tolist()
#Add the list of frequencies to the frequency column
dataf['freq'] = freq

freq2 = np.arange(7,70,1).tolist()

for i, value in enumerate(freq2, len(dataf)):
    dataf.loc[i] = [np.nan, value, np.nan]

I hope this helps to solve your problem.我希望这有助于解决您的问题。

暂无
暂无

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

相关问题 从 pandas 空 dataframe 创建一个只有列名的字典 - Create a dictionary from pandas empty dataframe with only column names Pandas 创建只有列名的空 DataFrame - Pandas create empty DataFrame with only column names 我如何获取两个列表 Pandas DataFrame 列名,并且只使用一个列表,但 append 一个循环中的字符串应用于列名? - How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a string in a loop to be applied to the column name? 用空数组填充数据框列 - Fill a dataframe column with empty arrays Pandas数据帧列表列为每列的numpy数组 - Pandas dataframe columns of lists to numpy arrays for each column 使用 pandas DataFrame 中列表列的名称展开元组列 - Expand column of tuples with names from column of lists in pandas DataFrame 如果其他两个列在Pandas中具有匹配的值,如何用另一个数据框的值填充空列的值? - How to fill empty column values with another dataframe's value if two other columns have matching values in Pandas? 仅将值复制到具有列名称的新的空数据框-Pandas - Copy values only to a new empty dataframe with column names - Pandas 如何用熊猫中现有列之一中的列表中的名称来创建新列,并从另一列中的列表中分配值? - How to make new columns with names from lists which are in one of existing columns in pandas, and assign values from lists from another column? 熊猫用以前的值填充空列名 - Pandas fill empty column names with previous value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM