简体   繁体   中英

Can I assign subsequent columns of Pandas dataframe to tuple of variables?

I need to assign the columns of data frame to variables and I wondered if there's a one-liner to do that. So let's say my data frame is:

df = pd.DataFrame.from_dict({'var1': {0: -3.992865426199777,
  1: -5.959995814732142,
  2: -0.8014242989676339,
  3: -1.294283185686384,
  4: -1.318566458565848},
 'var2': {0: 87.0469970703125,
  1: 69.947998046875,
  2: 67.12300109863281,
  3: 100.9739990234375,
  4: 70.27300262451172}})

And I'm seeking for something like:

var1, var2 = df[["var1", "var2"]]

Which is obviously incorrect. How can I accomplish that?

Use:

var1, var2 = df["var1"], df["var2"]
print (var1)
0   -3.992865
1   -5.959996
2   -0.801424
3   -1.294283
4   -1.318566
Name: var1, dtype: float64

print (var2)
0     87.046997
1     69.947998
2     67.123001
3    100.973999
4     70.273003
Name: var2, dtype: float64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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