简体   繁体   English

如何从具有复杂结构的列表中创建 DataFrame?

[英]How to create a DataFrame from a list with complex structure?

I have a dataframe with multiple columns (a, b, c, d).我有一个 dataframe 多列(a,b,c,d)。 I grouped my dataframe by columns 'c' and 'd' and integrated column 'a' with respect to column 'b'.我将我的 dataframe 按“c”和“d”列以及相对于“b”列的“a”列进行了分组。 This gave me the output in the following format (the values which go like '1500, 1400... 700' are the result of the performed integration):这给了我以下格式的 output (go 之类的值 '1500, 1400... 700' 是执行集成的结果):

c d
1 10  1500
  20  1400
  30  1300
2 10  1200
  20  1100
  30  1000
3 10   900
  20   800
  30   700

I was wondering how can I convert that output into a dataframe, where it would show columns 'c', 'd', and also create a new column which would contain the result of integration?我想知道如何将 output 转换为 dataframe,它会显示列“c”、“d”,并创建一个包含集成结果的新列?

Please try:请试试:

df = df.reset_index()

I recently created a DataFrame from similar data.我最近根据类似数据创建了 DataFrame。

First, create simple lists for each c-value of the complex list.首先,为复杂列表的每个 c 值创建简单列表。

c1_vals = [1500, 1400, 1300], etc

Create a DataFrame with a dictionary with c-values as keys and lists as values.使用字典创建 DataFrame,其中 c 值作为键,列表作为值。

ex_df = pd.DataFrame({'1': c1_vals, '2': c2_vals, '3': c3_vals})

The d-values from the complex-data correspond to each lists' index position.来自复数数据的 d 值对应于每个列表的索引 position。

ex_df['1'][0] represents c:1 d:10

You could create variables for individual d-values like this.您可以像这样为单个 d 值创建变量。

c1d10 = ex_df['1'][0]

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

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