How can I line a Pandas dataframe from a list that contains variables?

Asked

Viewed 23 times

2

So guys, I’m having a problem that basically works in a simplified way like this

# Eu criei um DataFrame a partir do pandas
import pandas as pd
Colunas = ["A","B","C","D"]
df = pd.DataFrame(columns = Colunas)

And let’s say I have a list of initial values

DadosIniciais = [X,Y,Z,W]

where XYZW are float values that arise from simple mathematical operations. What would be the most effective method to add this list of XYZW values as the first line of Dataframe?

  • If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

1 answer

1

One possibility is to create Dataframe from a list dictionary, as indicated in this question.

In your case, we would have:

import pandas as pd
import random
Colunas = ["A","B","C","D"]
X=random.random()
Y=random.random()
Z=random.random()
W=random.random()
Dadosiniciais=[X,Y,Z,W]

df = pd.DataFrame({k:[v] for k, v in zip(Colunas,Dadosiniciais)})

print(df)

Returns:

          A         B         C         D
0  0.437675  0.321801  0.847752  0.480337

Browser other questions tagged

You are not signed in. Login or sign up in order to post.