【问题标题】:dataframe match column数据框匹配列
【发布时间】:2021-07-03 23:11:35
【问题描述】:

我有这个 DF: Columns: df=pd.DataFrame(columns=["a","b","c","d","e","f","g"])

还有这个:data=["a:42","b:43","c:22","d:41","a:21","b:14" ,"c:12","e:14" ,"f:7","a:0" ,"d:1","f:3","a:6" ,"b:0","c:9","g:8" ]

我需要

for d in data:
   spli=d.split(":")
   colum=spli[0]
   value=spli[1]
   df[colum] = value

等待这个结果

["a"  "b"   "c"   "d"   "e"    "f"    "g"  ]
  42   43    22    41   nan    nan     nan
  21   14    12    nan   14     7      nan
  0    nan   nan    1    nan     3     nan
  6     0     9    nan   nan   nan      8

【问题讨论】:

  • 新行是否总是以'a'开头?或者新的一行可以从别的东西开始吗?
  • 如果“a”不存在我会用nan填充它,但“a”大部分时间存在

标签: python python-3.x pandas dataframe numpy


【解决方案1】:

您需要一个非空的 df 来将列设置为某个值。您可以为包含所有 nan 的行创建一个新的 df,然后设置列值。您可以通过比较 a、b、c ... 的数值与 ord() 来测试是否需要新行。将 df_row 附加到每个新行的主 df,并在循环结束时附加一次。这是一种方法:

df = pd.DataFrame(columns=["a", "b", "c", "d", "e", "f", "g"])

data = ["a:42", "b:43", "c:22", "d:41", "a:21", "b:14", "c:12", "e:14", "f:7", "a:0", "d:1", "f:3", "a:6", "b:0", "c:9", "g:8"]

df_this_row = pd.DataFrame([[np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN]], columns=["a", "b", "c", "d", "e", "f", "g"])
first_col, first_val = data[0].split(':')
df_this_row[first_col] = int(first_val)

for i in range(1, len(data)):
    col, val = data[i].split(':')
    prev_col = data[i-1].split(':')[0]
    if ord(col) <= ord(prev_col):     
        # you are in next row, eg f was previous col, and you have col b
        df = df.append(df_this_row)
        df_this_row = pd.DataFrame([[np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN, np.NAN]], columns=["a", "b", "c", "d", "e", "f", "g"])
    df_this_row[col] = int(val)

df = df.append(df_this_row).reset_index(drop=True)

print(df)
#     a    b    c    d    e    f    g
# 0  42   43   22   41  NaN  NaN  NaN
# 1  21   14   12  NaN   14    7  NaN
# 2   0  NaN  NaN    1  NaN    3  NaN
# 3   6    0    9  NaN  NaN  NaN    8

【讨论】:

    【解决方案2】:

    .loc 可以在这里为您提供帮助:

    last_c = 'z'   # Enter some ordinally large string 
    r = -1
    for x in data:
        c,v = x.split(':')
        
        if c <= last_c:
            r += 1
            
        df.loc[r,c] = v
        last_c = c
    
         a    b    c    d    e    f    g
    0   42   43   22   41  NaN  NaN  NaN
    1   21   14   12  NaN   14    7  NaN
    2    0  NaN  NaN    1  NaN    3  NaN
    3    6    0    9  NaN  NaN  NaN    8
    4  NaN   14    9  NaN  NaN  NaN  NaN
    

    我添加了一个额外的行来显示一个以 'a' 以外的内容开头的新行(行)。

    【讨论】:

    • 这个解决方案对我有用,但有一些变化!!!!谢谢
    【解决方案3】:

    是否可以将您的数据转换为字典列表,例如:

    data = [{"a":42,"b":43,"c":22,"d":41}, 
     {"a":21,"b":14 ,"c":12,"e":14 ,"f":7},
     {"a":0 ,"d":1,"f":3},
     {"a":6 ,"b":0,"c":9,"g":8}]
    

    然后,您可以通过以下方式创建数据框:

    df=pd.DataFrame(data).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 2016-08-26
      • 2021-08-08
      • 2020-04-29
      相关资源
      最近更新 更多