pandas 制作单行数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49602671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make One-Row Dataframe
提问by EHB
I'm unable to construct a dataframe from 3 individual numbers. I want to do this in order for a function to return the dataframe, which I then append to other existing results.
我无法从 3 个单独的数字构建数据框。我想这样做是为了让函数返回数据帧,然后我将其附加到其他现有结果。
Desired result is a dataframe with columns named "a", "b" and "C", each containing the value of a, b, and c.
所需的结果是一个包含名为“a”、“b”和“C”的列的数据框,每个列都包含 a、b 和 c 的值。
Try one:
尝试一:
a=1
b=2
c=3
dat=pd.DataFrame([a,b,c], columns=list('abc')) #fails with size error
Try two:
尝试两个:
dat=pd.DataFrame()
dat['a']=pd.np.nan
dat['b']=pd.np.nan
dat['c']=pd.np.nan
dat['c']=c # no numbers are added to the column; still has 0 rows
What am I missing here?
我在这里错过了什么?
Desired result is:
想要的结果是:
a | b | c
-------------
1 | 2 | 3
采纳答案by cs95
pd.DataFrame([[a, b, c]], columns=list('abc'))
a b c
0 1 2 3
Note that your "bonus ask" isn't really possible, because an object may be associated with multiple variables (think about it).
请注意,您的“奖金询问”实际上是不可能的,因为一个对象可能与多个变量相关联(想想看)。
You may, however, consider using a dictionary.
但是,您可以考虑使用字典。
data = {'a' : 1, 'b' : 2, 'c' : 3}
pd.DataFrame(data, index=[0]) # the `index` argument is important
a b c
0 1 2 3
回答by YOBEN_S
Notice , you should follow what cold and jpp's construction for creating the one row dataframe, but here I am try to fix your code. change DataFrame call to
请注意,您应该遵循冷和 jpp 的构造来创建一行数据框,但在这里我尝试修复您的代码。将 DataFrame 调用更改为
pd.Series([a,b,c], index=list('abc')).to_frame(0).T
Out[15]:
a b c
0 1 2 3
回答by jpp
Single append
单个追加
For a single append, there is no need to create a separate dataframe to append.
对于单个追加,无需创建单独的数据帧进行追加。
# ensure columns ordered appropriately.
df = df[['a', 'b', 'c']]
# define values
values = [1, 2, 3]
# add values to row at end of dataframe
df.loc[len(df.index)+1] = values
Multiple appends
多个附加
Continually appending dataframes is extremely inefficient.
不断追加数据帧的效率极低。
A much better idea is to append your results to a list, to form a list of a lists. Then create a dataframe from this list of lists, and append to the original. This is because appending to a list is considerably cheaper than appending to a dataframe.
一个更好的主意是将您的结果附加到一个列表中,以形成一个列表列表。然后从这个列表列表中创建一个数据框,并附加到原始列表中。这是因为附加到列表比附加到数据帧要便宜得多。
Suppose you have some values in an iterable iter_values
containing data you wish to append. Each item in the iterable is a list of 3 numbers.
假设您在iter_values
包含要附加的数据的可迭代对象中有一些值。可迭代对象中的每一项都是 3 个数字的列表。
lst = []
# loop through iterable, adding items to list
for values in iter_values:
lst.append(values)
# create dataframe from list of lists
df_append = pd.DataFrame(lst, columns=list('abc'))
# append to original dataframe, ignoring index
df = df.append(df_append, ignore_index=True)