1

I have defined such a dataframe:

from pyspark.sql.types import StructType, StructField, IntegerType, StringType
schema = StructType([
    StructField("date", StringType(), True), StructField("amount", StringType(), False), StructField("fontred", StringType(), False)
])

and I have such a list:

Elements=['01.07.', '01.07.', '02.07.', '02.07.', '02.07.', '02.07.']

How can I add elements of this list to first column of dataframe?

date      amount  fontred
----------------------------
01.07.
01.07.
02.07.
02.07.
02.07.
02.07.

2 Answers 2

1

I think you've confused the nullable parameter... I've changed it for you. See if the code below is what you want:

from pyspark.sql.types import StructType, StructField, IntegerType, StringType

schema = StructType([
    StructField("date", StringType(), False),
    StructField("amount", StringType(), True),
    StructField("fontred", StringType(), True)
])

Elements = ['01.07.', '01.07.', '02.07.', '02.07.', '02.07.', '02.07.']

df = spark.createDataFrame([[i,None,None] for i in Elements], schema=schema)

df.show()
+------+------+-------+
|  date|amount|fontred|
+------+------+-------+
|01.07.|  null|   null|
|01.07.|  null|   null|
|02.07.|  null|   null|
|02.07.|  null|   null|
|02.07.|  null|   null|
|02.07.|  null|   null|
+------+------+-------+
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer, if use your logic for amount -> column: df = spark.createDataFrame([[None,i,None] for i in Elements], schema=schema) then I loose information in date column. do you have any Idea what should i do?
If you have multiple columns that you want to combine, you can do, for example, df = spark.createDataFrame([[i,j,None] for i,j in zip(Elements,AnotherElements)], schema=schema)
0

You can use a pandas dataframe:

import pandas as pd
import numpy as np

Elements = ['01.07.', '01.07.', '02.07.', '02.07.', '02.07.', '02.07.']
df = pd.DataFrame({'date': Elements})
df['amount'] = df['fontred'] = np.nan
print(df)

Output:

     date  amount  fontred
0  01.07.     NaN      NaN
1  01.07.     NaN      NaN
2  02.07.     NaN      NaN
3  02.07.     NaN      NaN
4  02.07.     NaN      NaN
5  02.07.     NaN      NaN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.